Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/175351-finding-nearby-cities/
Share on other sites

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.