Main
Index
Search
Posts
Who's
Online
Log
In

Home: Products: CMS Builder: Plugins & Add-ons:
Geocoder - record A is xx miles from record B

 

 


rjbathgate
User

Jul 6, 2011, 5:12 PM

Post #1 of 3 (413 views)
Shortcut
Geocoder - record A is xx miles from record B Can't Post

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


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:


Code
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/ 


rjbathgate
User

Jul 7, 2011, 1:47 PM

Post #3 of 3 (403 views)
Shortcut
Re: [Jason] Geocoder - record A is xx miles from record B [In reply to] Can't Post

perfect, works a treat, thank you.

Just for info for anyone else, to convert the js to miles, change the var R = to var R = 3959 (this is the radius of the earth in miles)