Geolocation distance from current location

You must Login before you can answer or comment on any questions.

I'm trying to find an example of how to calculate the distance from the current location to a POI. I have a list of POIs that I need to display in a tableview along with the distance value based on the current location. Thanks

3 Answers

var velo = {};
velo.getDistance = function(lat1,lon1,lat2,lon2) {
   var R = 6371000000; // m (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;
   return Math.round(d);
};

— answered 3 years ago by Rainer Schleevoigt
answer permalink
2 Comments
  • Rainer, what is the unit here ? m, Km ? Thanks

    — commented 2 years ago by Armindo Da Silva

  • it is in millimeters (mm) =approx. 1/25 th of an inch, to get the results in meters drop 3 zeroes from var R, to get the results in kilometers drop 6 zeroes from var R.

    — commented 2 years ago by Harm Veenstra

Answered well here.

— answered 3 years ago by Rob Reid
answer permalink
1 Comment
  • If you try to use google's web services to get the distance over routes (street distance), you get a error message 'DENIED' ;-)) In web view it works fine.

    — commented 3 years ago by Rainer Schleevoigt

Your Answer

Think you can help? Login to answer this question!