distance of a polyline

倾然丶 夕夏残阳落幕 提交于 2019-12-01 01:43:19

The "geometry" library has a computeDistanceBetween method.

Something like this (not tested) should return the result in meters:

var polylineLength = 0;
for (var i = 0; i < markersPath.length; i++) {
  var lat = parseFloat(markersPath[i].getAttribute("lat"));
  var lng = parseFloat(markersPath[i].getAttribute("lng"));
  var pointPath = new google.maps.LatLng(lat,lng);
  path.push(pointPath);
  if (i > 0) polylineLength += google.maps.geometry.spherical.computeDistanceBetween(path[i], path[i-1]);

}
alert("the length of the polyline is "+polylineLength+" meters");

It's easy - using built in functions in the geometry library...

polyLengthInMeters = google.maps.geometry.spherical.computeLength(yourPolyline.getPath().getArray());

To use the geometry library you declare it when you load the map api

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key={YOUR_KEY}&amp;sensor=false&amp;libraries=geometry"></script>

for more info see:

Google API Polyline reference

Google API mcvArray reference

Google API Spherical geometry reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!