有人知道如何在google maps v3中获取折线的长度吗?目前我正在使用Haversine公式,但如果折线到达起点,则计算最短长度。有人知道我如何计算折线长度吗?
谢谢。 马克
谢谢。 马克
使用以下功能扩展Google Maps API:
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;
}
然后你可以调用:
var length_in_km = yourPoly.inKm();
来源:http://groups.google.com/forum/#!topic/google-maps-js-api-v3/Op87g7lBotc
这是一篇关于Google Maps JavaScript API v3的问题的帖子。用户报告说,他们在使用API时遇到了一个问题:当他们在地图上放置大量标记时,浏览器会崩溃或变得非常缓慢。使用Google几何库:
<script type="text/javascript" src="//maps.google.com/maps/api/js?v=3.exp&libraries=geometry"></script>
然后就像这样简单:
var meters = google.maps.geometry.spherical.computeLength(myPolyline.getPath());