Calculating a distance in miles between 2 coordinates

Advert test
#1
Hi,
I have been asked to build a site where users would search for a type of business within a ?? number of miles from X point, let say 20 miles radius from Kildare for instance.
If I have a database of towns / cities with their respective coordinates, does any one know a way to run this search??, well the theory beind it!!
 
#2
Not sure on the technicalities of this, but you might find some info on how the search engines do it for their local services by heading over to seobythesea.com. It's a site run by Bill Slawski that contains info on patents awarded to the SEs. You might find something there and if not try dropping Bill an email - he seems like a pretty cool guy.
 

mneylon

Administrator
Staff member
#3
It would be really cool if the property sites started offering that kind of service.

I honestly don't care which county a house is located in - I'm only interested in the distance from it to my office :)
 
#4
I'm pretty sure the new ask.com local service (cant remember the name...) allows this.

Most of the local services are tied into mapping now and you can map your route with distances given. Of course that's for the US, so we can expect similar for Ireland in, um, well, in about 10 years or so :)
 
#5
If you have the longtitude and latitude then google something like "calculating longtitude latitude distance".
It is basically Pythagorean math, and then changing to miles.
 

kae

New Member
#6
There was a post about something similar over at Pete Freitag's website:
Calculating Distance in Miles from Latitude and Longitude

Basically, you can use Pythagorus's theory as suggest by David (the link above shows a way), as long as the Longitudinal lines are relatively similar at the northenmost and southernmost latitudes that you will be considering.

Otherwise, you need to use more complex trigonometry to account for the curvature of the earth, as shown in this page:
Distance Calculation latitude longitude global database lists
 

kae

New Member
#8
I just found a very cool related trick.

Suppose you need to find all locations that are within 'n' kilometers of [x,y]. The "mostly accurate" way (ignoring the latitude problem) to do it is to "select * from locations where sqrt((x-@x)*(x-@x)+(y-@y)*(y-@y)) < @n". However, that's very slow, as you're doing math on every row of the database.

instead, you search within a bounding rectangle which is the same height/width as the n*2. This involves simple < and > checks. The result set from that will be very small, allowing you to do the accurate query on just the results. Very fast indeed.

If you have a recent MySQL (or any other db), you can do the bounding rectangle query as a subquery of the main one, letting you do a very fast check in one line!

The original article is here: Arjen's Journal - Great circle and distances
 
Top