2013-08-21 17 views
1

我正在根據經緯度來拖動用戶移動,因此最後1小時我獲得了超過20個座標。使用該座標,我使用谷歌地圖api繪製地圖。我得到了一條捲曲線(用戶移動圖)我想要得到從凝視點到終點的距離。如何獲得在google map api中映射的一組地理位置點的旅行距離?

這些都是我的座標: -

[[12.938419, 77.62224], 
[12.938494, 77.622377], 
[12.938369, 77.622449], 
[12.938345, 77.622521], 
[12.938322, 77.622575], 
[12.938346, 77.622631], 
[12.938306, 77.622648], 
[12.938299, 77.622695], 
[12.938254, 77.622715], 
[12.938242, 77.622761], 
[12.938227, 77.622805], 
[12.93819, 77.622792], 
[12.938138, 77.622837], 
[12.938129, 77.622887], 
[12.938103, 77.622949], 
[12.938066, 77.622989], 
[12.938006, 77.622966], 
[12.937933, 77.623001], 
[12.937976, 77.623073], 
[12.937954, 77.623128], 
[12.937912, 77.623111], 
[12.937882, 77.623034], 
[12.937933, 77.623001], 
[12.938006, 77.622966], 
[12.937921, 77.62293]] 

回答

2

獲取使用google.maps.geometry.spherical.computeDistanceBetween每組點之間的距離(每點轉換爲google.maps.LatLng第一)。總計這些距離。

或者對從座標創建的google.maps.LatLng對象數組使用google.maps.geometry.spherical.computeLength方法。

fiddle

for (var i=0; i < coordinates.length; i++) { 
    path.push(new google.maps.LatLng(coordinates[i][0], 
            coordinates[i][1])); 
} 
var polyline = new google.maps.Polyline({ 
    path: path, 
    map: map, 
    strokeWeight: 2, 
    strokeColor: "blue" 
}); 
var length = google.maps.geometry.spherical.computeLength(polyline.getPath()); 
document.getElementById('length').innerHTML = "length="+(length/1000).toFixed(2)+ " km"; 
+3

或者可以只LatLngs的數組傳遞給'computeLength'來計算一氣呵成的完整路徑。 – duncan

相關問題