2013-02-12 58 views
0

我有一個頁面,我試圖在地圖上模擬船隻位置。其中一些船隻在一條折線上的位置上佔有一定比例,另一些位於單點標記處。谷歌地圖:附加折線圖標上的事件

對於沿折線方式的百分比,我可以正確顯示圖標。我想要做的僅僅是將事件附加在該圖標上,而不是整個折線上。

我的代碼是在 http://www.cleancruising.com.au/map_allShipLoc2.asp

我也嘗試用google.maps.geometry.spherical.computeDistanceBetween交替地計算船舶位置的經緯度,但因爲這使用球面幾何,分別在平面地圖上不能正確翻譯。

回答

1

對於計算船舶位置的經緯度,見this thread from the Google Maps API v3 groupexample in it

下面是Mike Williams' (v2) epoly library

// === A method which returns a GLatLng of a point a given distance along the path === 
// === Returns null if the path is shorter than the specified distance === 
google.maps.Polygon.prototype.GetPointAtDistance = function(metres) { 
    // some awkward special cases 
    if (metres == 0) return this.getPath().getAt(0); 
    if (metres < 0) return null; 
    if (this.getPath().getLength() < 2) return null; 
    var dist=0; 
    var olddist=0; 
    for (var i=1; (i < this.getPath().getLength() && dist < metres); i++) { 
    olddist = dist; 
    dist += this.getPath().getAt(i).distanceFrom(this.getPath().getAt(i-1)); 
    } 
    if (dist < metres) {return null;} 
    var projection = this.getMap().getProjection(); 
    if (!projection) { 
    alert("no projection"); 
    return; 
    } 
    // Project 
    var p1= projection.fromLatLngToPoint(this.getPath().getAt(i-2)); 
    var p2= projection.fromLatLngToPoint(this.getPath().getAt(i-1)); 
    var m = (metres-olddist)/(dist-olddist); 
    // alert("p1="+p1+" p2="+p2+" m="+m); 
    // Unproject 
    return projection.fromPointToLatLng(new google.maps.Point(p1.x + (p2.x-p1.x)*m, p1.y + (p2.y-p1.y)*m)); 
} 
+0

改性的改性GetPointAtDistance功能現在通過這個展望。我認爲這會很好地工作。謝謝 – user2053170 2013-02-12 02:19:09