gpt4 book ai didi

google-maps-api-2 - 如何在 Google map 中使用步行旅行模式将多段线捕捉到道路?

转载 作者:行者123 更新时间:2023-12-03 23:39:17 24 4
gpt4 key购买 nike

我希望用户在 GoogleMaps 上用多段线绘制一条路线。我找到了一种捕捉折线的方法(链接中的示例,下面的代码)。该代码与方向服务一起在道路上绘制折线,唯一的问题是这仅适用于 G_TRAVELMODE_DRIVING 但我想使用 G_TRAVELMODE_WALKING 并且当您使用 WALKING 时,您必须在方向的构造函数中提供 map 和 div目的。当我这样做时,它会自动删除最后一行并仅显示当前行。我已经尝试了几种方法,比如将 map 提供为 null,或者在构造函数中省略 map 。我还尝试在构造函数中提供第二张 map ,从方向服务中获取多段线并将它们显示在右侧 map 。但是没有任何效果!如果有人能帮助我,我将不胜感激!

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");
}

最佳答案

我知道这是一篇旧帖子,但由于没有出现任何答案,而且我最近一直在研究这类代码(并使其正常工作),请尝试用它交换 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});
}
}
});

关于google-maps-api-2 - 如何在 Google map 中使用步行旅行模式将多段线捕捉到道路?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4681146/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com