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"
}