You are here: Re: Zip code « PHP Programming Language « IT news, forums, messages
Re: Zip code

Posted by Michael Phipps on 10/10/30 11:23

>I have a database of zipcodes with latitude and longitude. I also have the
>method of calculating the distance between two zipcodes. What I want to
>know is if there is an efficient algorithm for obtaining the zip codes
>within a specified distance of the first zipcode without having to retrieve
>and calculate for every record in the database.

How about a single select statement?

In this case I use a table called cities, where I have the fields city,
latitude and longitude. I have indexed the lat / long for improved speed.

$sqlstr="select city, ((acos(sin((latitude)*(pi()/180)) *
sin((".$latitude.")*(pi()/180)) + cos((latitude)*(pi()/180)) *
cos((".$latitude.")*(pi()/180)) * cos((longitude -
".$longitude.")*(pi()/180))))*(180/pi())* 60 * 1.1515 * 1.609344) dist,
latitude, longitude from cities order by dist limit 30";

On my server this query returns results in under a second when the table is
indexed correctly.

This query is only returning the 30 closest cities to a latitude longitude,
but perhaps you can search where the dist is less than so many kilometers.

oh the *1.609344 at the end of the equation converts miles to kilometers.

I was originally thinking something much more complex was necessary, but it
isn't.

Hope it helps!

OH - where did you get the lat long database?

Michael

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация