2

I am trying to calculate the distance from a specific point along a route that has waypoints using Google Maps API.

I am able to calculate the distance of the whole route by providing the Long/Lat coordinates of each waypoint, and the origin and destination coordinates.

The waypoints are provided to me as Long/Lat coordinates.

  [
"35.09432240,32.97050240",
"35.09386580,32.97096570",
"35.09382740,32.97114400",
"35.09383910,32.97139410",
"35.09386240,32.97143070",
"35.09395080,32.97147070"
]

Convert the array into waypoints:

   var waypts = [
    {
      location: new google.maps.LatLng(35.09424080, 32.97147740),
      stopover: true
    },
    {
      location: new google.maps.LatLng(35.09465580, 32.97141570),
      stopover: true
    },
    {
      location: new google.maps.LatLng(35.09473570, 32.97145240),
      stopover: true
    },
    {
      location: new google.maps.LatLng(35.09474740, 32.97147910),
      stopover: true
    }, 
  ];

I need a method to know where the current location (Long/Lat) is array waypoint coordinates.

here is an example of what I have created so far: https://jsfiddle.net/0ej7a5zk/

Basically the origin in the directionsService will change at a certain interval, and based on the origin coordinates, i will need to remove the waypoints that have already passed.

   directionsService.route({
    origin: new google.maps.LatLng(35.09395080,32.97147070),
    destination: new google.maps.LatLng(35.09468240,32.97180400),
    waypoints: waypts,
    optimizeWaypoints: true,
    travelMode: 'DRIVING'
  }, function(response, status) {
    if (status === 'OK') {
      directionsDisplay.setDirections(response);
      var route = response.routes[0];
      var summaryPanel = document.getElementById('directions-panel');
      summaryPanel.innerHTML = '';
      console.log("ROUTE: ", route)
      // For each route, display summary information.
      for (var i = 0; i < route.legs.length; i++) {
        var routeSegment = i + 1;
        summaryPanel.innerHTML += '<b>Route Segment: ' + routeSegment +
            '</b><br>';
        summaryPanel.innerHTML += route.legs[i].start_address + ' to ';
        summaryPanel.innerHTML += route.legs[i].end_address + '<br>';
        summaryPanel.innerHTML += route.legs[i].distance.text + '<br><br>';
      }

      computeTotalDistance(response);
    } else {
      window.alert('Directions request failed due to ' + status);
    }
  });
}

Any suggestions would be greatly appreciated

Ronny vdb
  • 2,324
  • 5
  • 32
  • 74
  • What do you mean by position? Do you mean the index of the element in the initial array? – max Aug 06 '19 at 14:18
  • If the current location is for example, "35.09383910,32.97139410", I will need to remove the first 3 items in the index or waypoints. By position I mean where if the current coordinates I get are before or after the waypoints along the route – Ronny vdb Aug 06 '19 at 14:20
  • [Array.prototype.find()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find) – max Aug 06 '19 at 14:24

0 Answers0