Calculating Distance Between 2 Cities
Solution 1
If you need to take the curvature of the earth into account, the Great-Circle distance is what you're looking for. The Wikipedia article probably does a better job of explaining how the formula works than me, and there's also this aviation formulary page that covers that goes into more detail.
The formulas are only the first part of the puzzle though, if you need to make this work for arbitrary cities, you'll need a location database to get the lat/long from. Luckily you can get this for free from Geonames.org, although there are commercial db's available (ask google). So, in general, look up the two cities you want, get the lat/long co-orinates and plug them into the formula as in the Wikipedia Worked Example.
Other suggestions:
- For a full commercial solution, there's PC Miler which is used by many trucking companies to calculate shipping rates.
- Make calls to the Google Maps (or other) api. If you need to do many requests per day, consider caching the results on the server.
- Also very important is to consider building an equivalence database for cities, suburbs, towns etc. if you think you'll ever need to group your data. This gets really complicated though, and you may not find a one-size-fits-all solution for your problem.
Last but not least, Joel wrote an article about this problem a while back, so here you go: New Feature: Job Search
Solution 2
You use the Haversine formula.
Solution 3
This is very easy to do with geography type in SQL Server 2008.
SELECT geography::Point(lat1, lon1, 4326).STDistance(geography::Point(lat2, lon2, 4326))
-- computes distance in meters using eliptical model, accurate to the mm
4326 is SRID for WGS84 elipsoidal Earth model
Solution 4
You can get the distance between two cities from google map api. Here is an implementation of it in Python
#!/usr/bin/python
import requests
from sys import argv
def get_distance(origin,destination):
gmap='http://maps.googleapis.com/maps/api/distancematrix/json'
payload={"origins":origin,"destinations":destination,"sensor":'false' }
try:
a=requests.get(gmap,params=payload)
data = a.json()
origin = str(data['origin_addresses'][0])
destination= str(data['destination_addresses'][0])
distance = data['rows'][0]['elements'][0]['distance']['text']
return distance,origin,destination
except Exception,e:
print "The %s or %destination does not exists :(" %(origin,destination)
exit()
if __name__=="__main__":
if len(argv)<3:
print "sorry Check the format"
else:
origin=argv[1]
destination=argv[2]
distance,origin,destination=get_distance(origin,destination)
print "%s ---> %s : %s" %(origin,destination,distance)
Example link: https://gist.github.com/sarathsp06/cf063e47bcc515b51c84
Solution 5
You ca use the A* algorithm to find the shortest path between those two cities and this way you'll have the distance.
Comments
-
Krishna Kumar almost 2 years
How do you calculate the distance between 2 cities?
-
Mikeon almost 15 yearsYour JoS link is out of date (clicking the link takes you to an error page now). I believe this is the right link now: joelonsoftware.com/items/2006/10/09.html
-
Dana the Sane almost 15 yearsI've updated the link and added some more info.
-
jpmc26 over 7 yearsIt's equally simple in PostGIS.