Calculating polyline length | formazzasport | 10/29/09 11:55 AM | Is there some methods to calculate polyline length?
thanks |

unk...@googlegroups.com | 10/29/09 4:48 PM | <This message has been deleted.> | |

Re: Calculating polyline length | Esa | 11/2/09 7:27 PM | That must be done segment by segment. First you need Haversine
distance method equal to distanceFrom() of v2. There is an unexposed method LatLng.Hj() that looks exactly like that. However it returned invalid results. Finally I found out that there were radian conversions missing. I imitated that method but added those conversions. 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; } Then another method loops through the polyline and increments the final length using the previous method. 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; } If the name of your polyline is poly, you can write: alert(poly.inKm()); The result is the length of the polyline in kilometers. The optional parameter is the path index of multi path polylines. I will put an example online one of these days. |

Re: Calculating polyline length | viper | 11/16/09 3:39 PM | Hi esa,
i tried the code you posted but i get an error at "var a = this.getPath (n)". The error description is "Object doesent support this property or method". I'm using IE7 my polyline name is "polyline" and i use this to get the length "alert (polyline.inKm())" any idea what might be wrong? |

Re: Calculating polyline length | Esa | 11/18/09 12:50 PM | I made some testing with IE7 and cannot reproduce the error. Script works with no errors. Even the parameter seem to have no effect with a single path polyline. A link to your page would be most informative. |