Welcome Guest, Not a member yet? Register   Sign In
Location, Location, Location (Proximity Search)
#1

[eluser]dpgtfc[/eluser]
Is there any good data for writing my own proximity search? I could use the experience, but I don't have the money to purchase the database with all of the data already. Is there an open source database or some other resources?

I'm guessing this would be more efficient to let MySQL do the bulk of the work rather than have PHP do the maths, right?
#2

[eluser]whitey5759[/eluser]
We needed something like this for a customer project. After much searching we discovered that, at the time (like a long time ago) no such open source database existed and the only way to get a hold of such data was to buy a database (which we did). Wasn't very expensive at all.

It contained millions of rows, each specifying the distance between 2 postcodes.

That being said things have changed. Wouldn't the Google Maps API support doing what you are after? It's free too Smile A couple of links which might help:

http://briancray.com/2009/04/01/how-to-c...-maps-api/

http://stackoverflow.com/questions/50674...e-maps-api

http://www.experts-exchange.com/Web_Deve...48760.html
#3

[eluser]BrianDHall[/eluser]
As luck would have it, I JUST implemented this but a few days ago. Shockingly easy! I was going to suffer through the Google API too as it would have cost around $100 for the libraries I found, then I found a real gem and got it working in CI in no time.

The Man - Micah Carrick. The Library - ...uh, Zipcode. The link: http://www.micahcarrick.com/04-19-2005/p...ation.html

Unzip it and I renamed the file to zipcode_class.php and put it in application/libraries.

Create the table as the instructions say, and there are like 5 SQL files to import - totalling about 40-some thousand zip codes in the US, complete with zipcode, city name, county, state, area code, timezone, and lat/lon coordinates. VERY useful. He states he got it from the 2005-2007 US Census, so is plenty fresh.

Here is the code for your very own CI demo - stick in any controller to get it going:

Code:
function test($zip1 = 32514, $zip2 = 32563, $range = 10)
    {
        $this->load->library('zipcode_class');

        $z = new zipcode_class;
        
        $miles = $z->get_distance($zip1, $zip2);

        if ($miles === false) echo 'Error: '.$z->last_error;
        else echo "Zip code <b>$zip1</b> is <b>$miles</b> miles away from <b>$zip2</b>.<br />";

        $result = $z->get_zips_in_range($zip1, $range);

        foreach ($result as $rzip => $rdist)
        {
            echo "$rzip is $rdist miles away<BR>";
        }
    }

get_zip_details() is a great function it has, too - gives you all the extra info it has on the chosen zipcode.

So to, say, display all stores in a given radius, you take the users zipcode, run get_zips_in_range($theirzip, $milerange), and it returns an array with the zipcodes and their distances from the provided zipcode.

Then you would take the zips, build it into a nice WHERE sql query, and grab out all your stores with zipcodes that match ones in the list. Presto change-o!

Just that easy - is way cool toy! Very handle in concert with google maps, which is what I'm doing - the less you have to use that damned google api, the better.
#4

[eluser]whitey5759[/eluser]
@BrianDHall - That's pretty cool! I took a look for interests sake, and noticed his site states 'The data itself is comprised of data from multiple sources starting with the 2000 and 2004 US Census data.'
#5

[eluser]BrianDHall[/eluser]
[quote author="whitey5759" date="1258537558"]@BrianDHall - That's pretty cool! I took a look for interests sake, and noticed his site states 'The data itself is comprised of data from multiple sources starting with the 2000 and 2004 US Census data.'[/quote]

Yes, that's the phrase I was thinking of. I believe elsewhere he said elsewhere somewhere he used Google's map API to pull the latitudes and longitudes. I might be making that up in my head, though - was working with 2 libraries at the same time, one for maps and one for zips.

I had found this: http://www.census.gov/geo/www/gazetteer/places2k.html

Free governmental census 2000 downloadable datasets, which include a link with zip code tabulation area data - but that only has <34k records, so seems to be less up to date than the free library.

For those who require total accuracy the postal service sells monthly up to date DVDs with all zipcode and delivery information. It's just not mission critical for me, so not worth the money Smile
#6

[eluser]benoa[/eluser]
Maybe my tiny geography_helper might help you to calculate the distance between two points

Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* Geography Helpers
*
*/

/**
* Distance
*/    
if ( ! function_exists('distance'))
{
    function distance($lat1, $lon1, $lat2, $lon2)
    {    
        return (int)(3958*3.1415926*sqrt(($lat2-$lat1)*($lat2-$lat1) + cos($lat2/57.29578)*cos($lat1/57.29578)*($lon2-$lon1)*($lon2-$lon1))/180);
    }
}

/* End of file geography_helper.php */
/* Location: ./system/helpers/geography_helper.php */

Note that the return number will be truncated as an int (it usually returns a float). You can wether use ceil() or floor() native PHP functions to get the accuracy you need. Casting as an int was good enough to me.

It's fairly easy to use it once you got place A & B latitudes, longitudes. Good luck.




Theme © iAndrew 2016 - Forum software by © MyBB