Get polyline length in google maps v3

Does anyone know how you can get the length of a polyline in google maps v3? I am currently using Haversine fomula, but if the polyline meets the start, then it calculates the shortest length. Does anyone know how I can calculate the length of a polyline?

Thank you very much. Mark

+4
source share
2 answers

Extend the Google Maps API with the following features:

google.maps.LatLng.prototype.kmTo = function(a){ var e = Math, ra = e.PI/180; var b = this.lat() * ra, c = a.lat() * ra, d = b - c; var g = this.lng() * ra - a.lng() * ra; var f = 2 * e.asin(e.sqrt(e.pow(e.sin(d/2), 2) + e.cos(b) * e.cos (c) * e.pow(e.sin(g/2), 2))); return f * 6378.137; } google.maps.Polyline.prototype.inKm = function(n){ var a = this.getPath(n), len = a.getLength(), dist = 0; for (var i=0; i < len-1; i++) { dist += a.getAt(i).kmTo(a.getAt(i+1)); } return dist; } 

and then you can call:

 var length_in_km = yourPoly.inKm(); 

source: http://groups.google.com/forum/#!topic/google-maps-js-api-v3/Op87g7lBotc

+16
source

Use Google Geometry:

 <script type="text/javascript" src="//maps.google.com/maps/api/js?v=3.exp&libraries=geometry"></script> 

Then it is so simple:

 var meters = google.maps.geometry.spherical.computeLength(myPolyline.getPath()); 
+4
source

Source: https://habr.com/ru/post/1332626/


All Articles