2010-12-18 52 views
4

有誰知道如何在google maps v3中獲取多段線的長度?目前,我正在使用Haversine公式,但如果多段線符合起點,則它會計算最短長度。有誰知道我如何計算折線長度?在google maps v3中獲取多段線的長度

非常感謝。 馬克

回答

16

延長谷歌地圖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

+2

真棒。答案應被接受。謝謝 – 2013-01-11 04:36:50

4

使用谷歌幾何:

<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()); 
相關問題