获取经度纬度坐标在坐标数组中的位置

时间:2019-08-06 14:04:26

标签: javascript latitude-longitude google-maps-direction-api

我正在尝试使用Google Maps API计算沿具有航路点的路线上距特定点的距离。

我能够通过提供每个航路点的经/纬度坐标以及起点和终点坐标来计算整个路线的距离。

航点是作为经/纬度坐标提供给我的。

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

将数组转换为航点:

   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
    }, 
  ];

我需要一种方法来知道当前位置(长/纬度)在哪里是数组航路点坐标。

这是到目前为止我创建的示例: https://jsfiddle.net/0ej7a5zk/

基本上,directionsService中的原点会以一定的间隔更改,并且基于原点坐标,我需要删除已经通过的航路点。

   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);
    }
  });
}

任何建议将不胜感激

0 个答案:

没有答案