Geocoder - record A is xx miles from record B

3 posts by 2 authors in: Forums > CMS Builder: Plugins & Add-ons
Last Post: July 7, 2011   (RSS)

By rjbathgate - July 6, 2011

Hello,

I have geocoded tables address and venue.

And want to display the distance between a record from each.

I.e. Your address is xx miles away from venue

Is that easily possible?

Thanks

Re: [rjbathgate] Geocoder - record A is xx miles from record B

By Jason - July 7, 2011

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 - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/