2011-09-21 217 views
1

我設法通過擴展getBounds方法居中多邊形路徑內的標誌:標記在多邊形路徑

//polyline 
google.maps.Polyline.prototype.getBounds = function() { 
    var bounds = new google.maps.LatLngBounds(); 
    this.getPath().forEach(function(e) { 
     bounds.extend(e); 
    }); 
    return bounds; 
}; 

所有我需要做的是這樣的:

var marker = new google.maps.Marker({ 
    map: map, 
    position: flight_path.getBounds().getCenter() 
}); 

現在我想的標誌達到10%,25%或95%的路徑。
我怎樣才能做到這一點?

回答

2

可以使用Geometry library工作了這一點。將Google地圖JS網址添加libraries=geometry

然後使用插值函數來工作,你將沿折線多大比例的標記。

var inBetween = google.maps.geometry.spherical.interpolate(startLatlng, endLatLng, 0.5); // 50% 

這是一個折線簡單,但如果你有多個線條形成一個路徑,它可能是一個有點麻煩!那麼也許你可以使用computeLength來計算總路徑長度,做數學自己對其中95%的是什麼,然後我不知道......

+0

將這項工作爲測地折線呢? – andufo

+0

是的,事實上,因爲它使用.spherical。類,它是專爲測地線 – duncan

+0

哇...這工作真的很好。謝謝! – andufo