开发者

Find users residing between a specified distance (using user's zipcode)

Is there a way to find the ra开发者_StackOverflowdial 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


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


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.


Google search found some good hits:

  • http://www.kevinbradwick.co.uk/2010/01/calculating-the-radius-of-a-coordinate-using-php/
  • http://www.phpclasses.org/package/3867-PHP-Lookup-United-States-zip-codes-in-a-MySQL-database.html


You can do all of this with just the database

  1. Download a free geocoded database of US zipcodes (like this one)
  2. Drop them into your database
  3. Write a simple sql function to compare two zips (i.e. ZIP_DIST(zip1,zip2) )
  4. Search against your list of peoples' zips and sort by the above function comparing everyone's zips to your target zip.
  5. Finally return the N closest values.

This way everything is run and cached at your MySQL layer. No API's needed.


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:

Find users residing between a specified distance (using user's zipcode)

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+"&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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜