
Jason
Staff
/ Moderator

Jul 7, 2011, 10:34 AM
Post #2 of 3
(405 views)
Shortcut
|
|
Re: [rjbathgate] Geocoder - record A is xx miles from record B
[In reply to]
|
Can't Post
|
|
Hi, Yes, you can calculate distance if you have the coordinates (latitude and longitude) of both points. Here is an example of a javascript function that returns this value:
function distance(lat1,lon1,lat2,lon2) { var R = 6371; // km (change this constant to get miles) var dLat = (lat2-lat1) * Math.PI / 180; var dLon = (lon2-lon1) * Math.PI / 180; var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(lat1 * Math.PI / 180 ) * Math.cos(lat2 * Math.PI / 180 ) * Math.sin(dLon/2) * Math.sin(dLon/2); var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); var d = R * c; if (d>1) return Math.round(d)+"km"; else if (d<=1) return Math.round(d*1000)+"m"; return d; } This code was taken from this page: http://www.barattalo.it/2009/12/19/ruler-for-google-maps-v3-to-measure-distance-on-map/ We haven't tested this function ourselves, and I believe it give you the distance in a straight line between the two points. Hope this helps get you started --------------------------------------------------- Jason Sauchuk - Programmer interactivetools.com Hire me! Save time by getting our experts to help with your project. http://www.interactivetools.com/consulting/
|