2015-03-25 46 views
2

我有問題。 我想計算當前位置和計算出的路線之間的距離。我正在使用Google Maps API v3來計算路線。我試圖使用google.maps.geometry.poly.isLocationOnEdge,但它沒有奏效。獲取當前位置和計算出的路線之間的距離

這裏是我的代碼:

directionsService.route(request, function(result, status) { 

    if (status == google.maps.DirectionsStatus.OK){ 

     var poly = new google.maps.Polyline({ 
      paths:new Array(result.routes[0].overview_path) 
     }); 

     var currentPosition = new google.maps.LatLng(51.574470000000005, 4.32713); 

     if(google.maps.geometry.poly.isLocationOnEdge(currentPosition, poly)){ 
      console.log("On route");  
     }else{ 
      console.log("Not on route"); 
     } 

     var directionsDisplay = new google.maps.DirectionsRenderer({ 
      polylineOptions: { 
       strokeColor: color, 
       strokeWeight: 3 
      } 
     }); 

     directionsDisplay.setMap(map); 
     directionsDisplay.setDirections(result); 

    } 
}); 

我不知道這是爲了得到我想要的結果的好方法。

有人可以幫助我嗎?

回答

0

首先,您可能需要在方法中設置公差。也許這一點只是一點點。默認容差是10^-9,非常小。有關更多詳細信息,請參見documentation

你真的需要距離路線嗎?或者足夠知道標記是否在您的路線上?

如果是這樣,你可能想看看這個similar question

這是建議有使用containsLocation()方法。

如果真要計算距離,然後我不得不說,根據this post

+0

由於這是不可能的!我要試一下。讓你保持最新! – 2015-03-26 19:02:09

+0

它的工作原理!問題在於概述路徑。 //檢查位置是在航線 \t \t \t \t如果(!google.maps.geometry.poly.isLocationOnEdge( \t \t \t \t \t新google.maps.LatLng(received_data [ '緯度'],received_data [ '經度']), \t \t \t \t \t新google.maps.Polyline({ \t \t \t \t \t \t路徑:result.routes [0] .overview_path}), \t \t \t \t \t \t .00001 * reach)){} – 2015-05-12 11:46:43

相關問題