views:

223

answers:

4

Is there a way to find the radial distance using the zipcode?

My task is to search the all the users residing with in a specified distance. I know the zipcodes of the users.

Eg, users between 25 miles from the current location.

I have other search categories, for which i am using mysql queries. I am unable to figure anything out for the distance issue.

My backend is in php and frontend in Flex.

The best option for me would be something like that of www.zip-codes.com/zip-code-radius-finder.asp. ie, if I should be able to get all the zip-codes available in the specified radial distance. So i can compare this zip-codes to the users zip-codes in my database. And select the ones with a match.

Please help me out with this. Zeeshan

+6  A: 

Postcodes don't map directly to a distance to each other. You will have to acquire postcode & lat/long data, look up the postcodes in there, and compare the distance between the lat/long coordinates. Depending on the locality free data may be available, but it's very common to buy such a table, or subscribe to a possibly paid webservice which does the translation of postcode to lat/long or neighboring postcodes for you.

Wrikken
+8  A: 

I accomplished something similar using the free Google Maps API - which may be good enough to use for your needs as well

http://googlemapsapi.blogspot.com/2006/06/geocoding-at-last.html - brief post on the subject (from 2006)

http://code.google.com/apis/maps/index.html - Google Maps API home

Eric C C
A: 

of course you can implement some of code from Google API, but here is a little project, I've wrote for you, and its look like below: btw i got one of services from google output like free-zipcode-maps.com/tools/zipcoderadius/extractzips2.cgi?Zip_Code=90503&Miles=30

here is code:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">

    <s:layout>
        <s:VerticalLayout/>
    </s:layout>

    <fx:Declarations>
        <s:HTTPService id="service" resultFormat="text" result="service_resultHandler(event)" fault="Alert.show(event.fault.toString())"/>
    </fx:Declarations>

    <s:HGroup width="100%" 
              horizontalAlign="center"
              verticalAlign="middle"
              textAlign="center">
        <s:Label text="ZIP code"/>
        <s:TextInput id="zip" text="90503"/>
        <s:Label text="Radius in miles"/>
        <s:TextInput id="miles" text="5"/>
        <s:Button label="GO!" click="button1_clickHandler(event)"/>
    </s:HGroup>
    <mx:DataGrid id="dg" width="100%" height="100%">
        <mx:columns>
            <mx:DataGridColumn dataField="zip" headerText="ZIP Code"/>
            <mx:DataGridColumn dataField="distance" headerText="{'Distance in Miles from '+zip.text}"/>
            <mx:DataGridColumn dataField="city" headerText="City"/>
            <mx:DataGridColumn dataField="state" headerText="State"/>
        </mx:columns>
    </mx:DataGrid>

    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.controls.Alert;
            import mx.rpc.events.ResultEvent;

            [Bindable]
            public var result:Array=new Array();

            protected function button1_clickHandler(event:MouseEvent):void
            {
                service.url="http://free-zipcode-maps.com/tools/zipcoderadius/extractzips2.cgi?Zip_Code="+zip.text+"&amp;Miles="+miles.text;
                service.send();             
            }

            protected function service_resultHandler(event:ResultEvent):void
            {   
                //<textarea name="download" cols=65 rows=20>
                //ZIP Code  Distance in Miles from 90503    City    State
                //</textarea>
                var res:String=event.result.toString();
                var startString:String="ZIP Code    Distance in Miles from "+zip.text+" City    State";
                var start:int=res.search(startString)+startString.length+1;
                var end:int=res.search('</textarea>')-1;
                res=res.substring(start,end);

                var rows:Array=res.split("\n");             
                for each(var row:String in rows)
                {
                    var r:Array=row.split("\t");
                    result.push({zip:r[0],distance:r[1],city:r[2],state:r[3]});
                }

                dg.dataProvider=new ArrayCollection(result);
            }

        ]]>
    </fx:Script>


</s:Application>
Eugene