2011-01-13 44 views
3

II希望用戶使用GoogleMaps上的多段線繪製路線。我找到了一種摺疊折線的方法(鏈接中的示例,下面的代碼)。該代碼與方向服務一起在道路上繪製折線,唯一的問題是這隻適用於G_TRAVELMODE_DRIVING,但我想使用G_TRAVELMODE_WALKING,並且當您使用WALKING時,必須在方向的構造函數中提供地圖和div目的。當我這樣做時,它會自動刪除最後一行並僅顯示當前行。我嘗試了幾種方法,比如將地圖提供爲null,或者在構造函數中省略地圖。我也嘗試在構造函數中給出第二個地圖,從方向服務中獲取多段線並將它們顯示在右側地圖。但沒有任何作品!如果有人能幫助我,我將不勝感激!如何在Google地圖中將多段線捕捉到具有步行travelmode的道路?

http://econym.org.uk/gmap/example_snappath.htm

if (GBrowserIsCompatible()) { 

    var map = new GMap2(document.getElementById("map")); 
    map.setCenter(new GLatLng(53.7877, -2.9832),13) 
    map.addControl(new GLargeMapControl()); 
    map.addControl(new GMapTypeControl()); 
    var dirn = new GDirections(); 

    var firstpoint = true; 
    var gmarkers = []; 
    var gpolys = []; 
    var dist = 0; 


    GEvent.addListener(map, "click", function(overlay,point) { 
    // == When the user clicks on a the map, get directiobns from that point to itself == 
    if (!overlay) { 
     if (firstpoint) { 
     dirn.loadFromWaypoints([point.toUrlValue(6),point.toUrlValue(6)],{getPolyline:true}); 
     } else { 
     dirn.loadFromWaypoints([gmarkers[gmarkers.length-1].getPoint(),point.toUrlValue(6)],{getPolyline:true}); 
     } 
    } 
    }); 

    // == when the load event completes, plot the point on the street == 
    GEvent.addListener(dirn,"load", function() { 
    // snap to last vertex in the polyline 
    var n = dirn.getPolyline().getVertexCount(); 
    var p=dirn.getPolyline().getVertex(n-1); 
    var marker=new GMarker(p); 
    map.addOverlay(marker); 
    // store the details 
    gmarkers.push(marker); 
    if (!firstpoint) { 
     map.addOverlay(dirn.getPolyline()); 
     gpolys.push(dirn.getPolyline()); 
     dist += dirn.getPolyline().Distance(); 
     document.getElementById("distance").innerHTML="Path length: "+(dist/1000).toFixed(2)+" km. "+(dist/1609.344).toFixed(2)+" miles."; 
    } 
    firstpoint = false; 
    }); 

    GEvent.addListener(dirn,"error", function() { 
    GLog.write("Failed: "+dirn.getStatus().code); 
    }); 

} 
else { 
    alert("Sorry, the Google Maps API is not compatible with this browser"); 
} 
+1

,如果你提供的鏈接,什麼*不*工作,這將是很有益的。或者甚至可能是代碼不起作用,而不是Mike Williams的代碼。 – 2012-03-13 14:12:06

回答

0

我知道這是舊的文章,但由於沒有答案上前和我最近一直在研究這種代碼(和得到它的工作),嘗試用這種調換的addListener 。

GEvent.addListener(map, "click", function(overlay,point) { 
    // == When the user clicks on a the map, get directiobns from that point to itself == 
    if (!overlay) { 
     if (firstpoint) { 
     dirn1.loadFromWaypoints([point.toUrlValue(6),point.toUrlValue(6)],{ getPolyline:true,travelMode:G_TRAVEL_MODE_WALKING}); 
     } else { 
     dirn1.loadFromWaypoints([gmarkers[gmarkers.length-1].getPoint(),point.toUrlValue(6)],{ getPolyline:true,travelMode:G_TRAVEL_MODE_WALKING}); 
     } 
    } 
    }); 
相關問題