# Calculate distance between two places on accounts from a VF page

I looking to find distance between 2 vendors(Accounts).

I do have the lattitude and longitude of the accounts. I think i could use the google maps API to get the distance. Anybody worked with google maps API from VF page? Any pointers here would great.

I also think google maps have limits on the no of requests made per IP or instance, so if there are many organizations using the na1 server and some of those also use the requests. would all those requests from other organizations would count in the limits?

Thanks

## 7 Answers

Our app, Geopointe, does have this built in. In addition to doing all the mapping you might want, we have an Apex method at geopointe.API.getDistance(Id1, Id2, geopointe.API.units.MILES). This gives you back the distance.

That will be far and away the simplest in the end.

If you want to write a method yourself, checkout the MapQuest Open Street Map API at http://open.mapquestapi.com/sdk/js/v7.0.s/mqa.toolkit.js. That's just a bunch of JavaScript, but do a find on "Util.distanceBetween" and you'll see a function that accepts 2 lat/lon objects and does some math. It's not too hard to reverse engineer.

You can also do a find for "arcDistance" in that toolkit. This is a method measuring the distance, but takes into account that we live on a sphere.

If JavaScript is ok for you, you can also checkout the MapQuest Open APIs. See http://developer.mapquest.com/web/documentation/open-sdk/javascript/v7.0. There are no major limits, especially since you already have lat/lon data. You don't even have to show a map. You could just use it to do the Calcs you need.

``````public Decimal calculateDistance(Decimal lat1, Decimal lon1, Decimal lat2,   Decimal lon2){
Double Radius = 6371.00;
Double dLat = toRadians(lat2-lat1);
Double dLon = toRadians(lon2-lon1);
Double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(toRadians(lat1)) *   Math.cos(toRadians(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2);
Double c = 2 * Math.asin(Math.sqrt(a));
return Radius * c * .62;
}

private Double toRadians(Decimal degree){
Double res = degree * 3.1415926 / 180;
return res;
}
``````

Below is a snippet of a function that calculates the distance in miles given the Origin and Target Latitude and Longitude in degrees as double.

Some of the optimization has been removed to make the logic clearer.

``````...
// convert to radians
Double dDepartLat = Origin_Latitude__c * 3.14159 / 180;
Double dDepartLong = Origin_Longitude__c * 3.14159 / 180;
Double dArrivalLat = Target_Latitude__c * 3.14159 / 180;
Double dArrivalLong = Target_Longitude__c * 3.14159 / 180;

Double dDeltaLong = dArrivalLong - dDepartLong;
Double dDeltaLat = dArrivalLat - dDepartLat;

// calculate angle using the haversine formula
Double dHaversineResult = Math.Sin( dDeltaLat / 2 ) * Math.Sin( dDeltaLat / 2 )
+ Math.Cos( dDepartLat ) * Math.Cos( dArrivalLat )
* Math.Sin( dDeltaLong / 2 ) * Math.Sin( dDeltaLong / 2 );

// calculate distance by multiplying arc-tangent by the planet radius in miles
Double dDistance = 3958.76 * 2 * Math.Atan2( Math.Sqrt( dHaversineResult ), Math.Sqrt( 1 - dHaversineResult ) );

// return estimate
return dDistance;
}
``````

A question like this was recently asked that might help you. Take a look here: Geolocation searching.

If you're still struggling with this, take a look at the new GeoLocation field type that SFDC are releasing with Winter '13. I think I saw, in a demo, that there'd be direct support for calculating distances between coordinates using Apex.

@richardvanhook implemented this as part of a CloudSpokes challenge. The code is in github: https://github.com/cloudspokes/SimpleGeo-Geolocation-Toolkit if you'd like to take a look.

I would suggest looking into something like Geopointe from the Appexchange.