skip to Main Content

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


  1. This code makes a request to the Google Maps Directions API and extracts the road distance from the response

    const origin = `${this.currentLocation.lat},${this.currentLocation.lon}`;
    const destination = `${this.tripDetail.pickUp.location.lat},${this.tripDetail.pickUp.location.lon}`;
    const apiKey = 'YOUR_API_KEY';
    
    // Make a request to the Directions API
    const response = await fetch(`https://maps.googleapis.com/maps/api/directions/json?origin=${origin}&destination=${destination}&key=${apiKey}`);
    const data = await response.json();
    
    // Extract the road distance from the response
    const distanceInMeters = data.routes[0].legs[0].distance.value;
    const distanceInKilometers = distanceInMeters / 1000;
    this.locationToRetailer = distanceInKilometers;
    
    Login or Signup to reply.
  2. 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:

    function calculateDistance(lat1, lon1, lat2, lon2) {
      // Create a DirectionsService object
      var directionsService = new google.maps.DirectionsService();
    
      // Define the origin and destination coordinates
      var origin = new google.maps.LatLng(lat1, lon1);
      var destination = new google.maps.LatLng(lat2, lon2);
    
      // Create a request object for the directions
      var request = {
        origin: origin,
        destination: destination,
        travelMode: google.maps.TravelMode.DRIVING // Specify the travel mode (DRIVING, WALKING, etc.)
      };
    
      // Call the DirectionsService route() method to calculate the directions
      directionsService.route(request, function(result, status) {
        if (status == google.maps.DirectionsStatus.OK) {
          // Retrieve the distance from the result object
          var distance = result.routes[0].legs[0].distance.value;
          // Distance is in meters, you can convert it to miles or kilometers as needed
    
          // Use the distance value as needed
          console.log("Road distance:", distance, "meters");
        } else {
          console.error("Error calculating road distance:", status);
        }
      });
    }
    

    Make sure to include the Google Maps JavaScript API script in your HTML file:

    <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"></script>
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search