1

我正在根据他们的纬度和经度来跟踪用户的移动。所以在过去的 1 小时里,我得到了 20 多个坐标。使用这些坐标,我正在使用谷歌地图 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]]
4

1 回答 1

2

使用google.maps.geometry.spherical.computeDistanceBetween获取每组点之间的距离(首先将每个点转换为 google.maps.LatLng)。总和这些距离。

或者对从您的坐标创建的 google.maps.LatLng 对象数组使用google.maps.geometry.spherical.computeLength方法。

小提琴

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";
于 2013-08-21T14:11:47.253 回答