mooders Posted September 24, 2009 Share Posted September 24, 2009 Hi, I am building a web app, part of which would benefit greatly from being able to determine towns and cities which fall within a given radius of a user-entered town. So, if I enter 'Denver', I want my app to be able to determine if another town, say 'Fort Collins', is within that pre-defined radius. Does anyone know of an API or library or database etc I can use to do this? Many thanks, Neil Quote Link to comment https://forums.phpfreaks.com/topic/175351-finding-nearby-cities/ Share on other sites More sharing options...
Adam Posted September 24, 2009 Share Posted September 24, 2009 Take a look at GeoIP. Quote Link to comment https://forums.phpfreaks.com/topic/175351-finding-nearby-cities/#findComment-924078 Share on other sites More sharing options...
bossman Posted September 24, 2009 Share Posted September 24, 2009 Dude...the google maps API is absolutely the best solution for what your trying to do... here is one i made... http://wesco.jp-clients.com/branch_search/index.php This is exactly what your trying to do from what you described... I don't have a direct link to the tutorial, but if you google "google maps API system" im sure you will find it, there are tutorials for geocoding the latitudes and longitudes as well. Hope this helps.. BOSSMAN Quote Link to comment https://forums.phpfreaks.com/topic/175351-finding-nearby-cities/#findComment-924138 Share on other sites More sharing options...
JonnoTheDev Posted September 24, 2009 Share Posted September 24, 2009 If it is your own database of cities/towns, you will need the longitude / latitude values for each to start with. You will probably need to write a data miner to obtain this information from wherever you can find it (wikipedia, fallingrain, etc). The next step is a bit tricky. You require a bit of mathematical magic, known as the Haversine formula for calculating the distance between lat & long points. You can run as a query i.e: <?php // get long and lat of a given place i.e. Sevilla, Spain $lat = 37.3772; $lon = -5.98694; // now find the nearest places within a given distance (miles) $distance = 12; $result = mysql_query("SELECT cityId, city FROM cities WHERE SQRT(POW(69.1 * (latitude - ".$lat."), 2) + POW(69.1 * (".$lon." - longitude) * COS(latitude / 57.3) , 2)) < ".$distance); ?> If you want to include the distance from the starting city for each record found or order by distance let me know and I will expand on the query. However this is your start point. Quote Link to comment https://forums.phpfreaks.com/topic/175351-finding-nearby-cities/#findComment-924163 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.