how to calculate the actual road distance between two latitude and longitude in angular
I use this but I want for actual road distance between two latitude and longitude
var radlat1 = Math.PI * this.tripDetail.pickUp.location.lat/180;
var radlat2 = Math.PI * this.currentLocation.lat/180;
var theta = this.tripDetail.pickUp.location.lon - this.currentLocation.lon;
var radtheta = Math.PI * theta/180;
var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
if (dist > 1) {
dist = 1;
}
dist = Math.acos(dist);
dist = dist * 180/Math.PI;
dist = dist * 60 * 1.1515;
// if (unit=="K") { dist = dist * 1.609344 }
// if (unit=="N") { dist = dist * 0.8684 }
this.locationToRetailer = dist * 1.609344
Please give me a solution…..
2
Answers
This code makes a request to the Google Maps Directions API and extracts the road distance from the response
To calculate the actual road distance between two latitude and longitude coordinates in JavaScript, you can use the Google Maps JavaScript API or any other mapping or routing service that provides directions and distance calculation. Here’s an example using the Google Maps Directions API:
Make sure to include the Google Maps JavaScript API script in your HTML file:
Remember to replace
YOUR_API_KEY
with your actual Google Maps API key. Additionally, ensure that you have enabled the Google Directions API for your API key.Note that using a mapping or routing service like Google Maps will give you the road distance, which is the driving distance along the available road network.