在谷歌地圖API V2很容易,折線捕捉到道路使用谷歌地圖API V3
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;
// == When the user clicks on a the map, get directiobns from that point to itself ==
gmarkers.push(new google.maps.LatLng(53.7877, -2.9832));
gmarkers.push(new google.maps.LatLng(53.9007, -2.9832));
gmarkers.push(new GLatLng(53.600, -2.700));
for (var i = 0; i < gmarkers.length-1; i++) {
console.log(gmarkers[i]);
dirn.loadFromWaypoints([gmarkers[i].toUrlValue(6),gmarkers[i+1].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();
map.addOverlay(dirn.getPolyline());
gpolys.push(dirn.getPolyline());
dist += dirn.getPolyline().getDistance();
document.getElementById("distance").innerHTML="Path length: "+(dist/1000).toFixed(2)+" km. "+(dist/1609.344).toFixed(2)+" miles.";
});
GEvent.addListener(dirn,"error", function() {
GLog.write("Failed: "+dirn.getStatus().code);
});
console.log(dirn);
在谷歌API V3這樣簡單行不通的。有類似的方向服務,但我沒有任何想法,我怎麼能通過我的點繪製折線,折線將被絆倒到道路上。
謝謝,乾淨,簡潔。 –
@ chad-killingsworth所以如果我正確地閱讀了這篇文章,您可以通過每次點擊調用DirectionsService,然後將所有這些結果存儲在數組中,來避開最大8個航點限制。假設我想每1英里增量自動顯示一個標記,類似於gmaps-pedometer.com;任何想法如何使用API做到這一點? –
對不起,無視上述問題的後半部分;在這裏找到答案:http://stackoverflow.com/questions/2698112/how-to-add-markers-on-google-maps-polylines-based-on-distance-along-the-line ..不完全直接和它看起來像它需要重新爲v3,但它看起來可以... –