0

Now i am using google distance matrix API to get distance from one location to another. Is it possible to get the same distance (transport) without using distance matrix API. I was tried the blow code for get the distance using latitude and longitude but didn't get the exact distance value like google distance matrix api.

alert(getDistanceFromLatLonInKm(50.886914964185600,-0.966288594404237,50.936471758083400,-0.999119111882654));
  var R = 6371; // Radius of the earth in km
  var dLat = deg2rad(lat2-lat1);  // deg2rad below
  var dLon = deg2rad(lon2-lon1); 
  var a = 
    Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
    Math.sin(dLon/2) * Math.sin(dLon/2)
    ; 
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
  var d = R * c; // Distance in km
  return d;
}

function deg2rad(deg) {
  return deg * (Math.PI/180)
}

Google Matrix Api Request:

https://maps.googleapis.com/maps/api/distancematrix/json?origins=50.886914964185600,-0.966288594404237&destinations=50.936471758083400,-0.999119111882654&mode=driving&language=en-EN&sensor=false&key={{key}}

Google Matrix Api Response:

   "destination_addresses" : [ "Green La, Clanfield, Waterlooville PO8 0JU, UK" ],
   "origin_addresses" : [ "79 Redhill Rd, Rowland's Castle PO9 6DE, UK" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "7.1 km",
                  "value" : 7120
               },
               "duration" : {
                  "text" : "9 mins",
                  "value" : 553
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}
Word Rearranger
  • 1,306
  • 1
  • 16
  • 25
Rahumathulla
  • 73
  • 10
  • Is google API taking into account road shapes, and you not? Try larger distances for removing such error. –  Dec 18 '19 at 15:09
  • I want alternative solution for distance calculation – Rahumathulla Dec 18 '19 at 15:14
  • "but didn't get the exact distance value like google distance matrix api." @rahum what did you get instead? –  Dec 18 '19 at 15:14
  • What is your result? Is it just a rounding issue? Does Google Maps considers the available streets and you just the direct connection? What is your expected result, yours or that from google? – Oliver Dec 18 '19 at 15:20

0 Answers0