Esa
unread,Nov 2, 2009, 10:27:33 PM11/2/09Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Sign in to report message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Google Maps JavaScript API v3
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.