gpt4 book ai didi

google-maps-api-3 - Gmaps 方向保存路线,航点

转载 作者:行者123 更新时间:2023-12-04 16:12:00 26 4
gpt4 key购买 nike

我有一张带有路线的 map (多个目的地 - 不仅仅是 A、B,如下面的页面所示)。方向是可拖动的。我想保存更改后的路线。下面的页面是我要去的...

http://vikku.info/programming/google-maps-v3/draggable-directions/saving-draggable-directions-saving-waypoints-google-directions-google-maps-v3.htm

这是我所拥有的:

google.maps.event.addListener(directionsDisplay, 'directions_changed', function() {
console.log('reroute');

var rleg_count = directionsDisplay.directions.routes[0].legs.length;

data.start = {
'lat':directionsDisplay.directions.routes[0].legs[0].start_location.lat(),
'lng':directionsDisplay.directions.routes[0].legs[0].start_location.lng()
};
data.end = {
'lat':directionsDisplay.directions.routes[0].legs[rleg_count-1].end_location.lat(),
'lng':directionsDisplay.directions.routes[0].legs[rleg_count-1].end_location.lng()
};

var wp=[];
var w = [];
var route = directionsDisplay.directions.routes[0];
for (var l = 0; l < route.legs.length; l++)
{
for(var j = 0; j < route.legs[l].via_waypoints.length; j++)
{
w.push({
location:{'lat':route.legs[l].via_waypoints[j].lat(), 'lng':route.legs[l].via_waypoints[j].lng()},
stopover:true
});
}
}

data.waypoints = w;
});

和 SetRoute 函数:
function setRoute(os)
{
var wp = [];
for(var i=0;i<os.waypoints.length;i++)
{
wp[i] = {
'location': new google.maps.LatLng(os.waypoints[i].location.lat, os.waypoints[i].location.lng),
'stopover': os.waypoints[i].stopover
}
}

var request = {
'origin':new google.maps.LatLng(os.start.lat, os.start.lng),
'destination':new google.maps.LatLng(os.end.lat, os.end.lng),
'waypoints': wp,
optimizeWaypoints: false,
avoidHighways: false,
avoidTolls: false,
travelMode: google.maps.TravelMode.DRIVING,
}

directionsService.route(request, function(res,sts){
// console.log(res);
// console.log(sts);
if(sts=='OK')
directionsDisplay.setDirections(res);
});
}

问题是,如果只有 2 个目的地(A、B),代码就可以工作,但是一旦有更多目的地……它就不能正常工作……它会忽略其他目的地

据我所知,航点是正确的。多个目的地似乎有问题,我不知道如何纠正。

谢谢!

最佳答案

我设法通过对我的代码进行以下更新来解决该问题:

google.maps.event.addListener(directionsDisplay, 'directions_changed', function() {
console.log('reroute');

var rleg_count = directionsDisplay.directions.routes[0].legs.length;

data.start = {
'lat':directionsDisplay.directions.routes[0].legs[0].start_location.lat(),
'lng':directionsDisplay.directions.routes[0].legs[0].start_location.lng()
};
data.end = {
'lat':directionsDisplay.directions.routes[0].legs[rleg_count-1].end_location.lat(),
'lng':directionsDisplay.directions.routes[0].legs[rleg_count-1].end_location.lng()
};

var wp=[];
var w = [];
var route = directionsDisplay.directions.routes[0];
for (var l = 0; l < route.legs.length; l++)
{
// Skip first and last leg - for more than 2 legs
if(l != 0 || l != rleg_count-1)
{
/* start solution */
w.push({
location:{'lat':route.legs[l].start_location.lat(), 'lng':route.legs[l].start_location.lng()},
stopover:true
});
}
/* end solution */
for(var j = 0; j < route.legs[l].via_waypoints.length; j++)
{
w.push({
location:{'lat':route.legs[l].via_waypoints[j].lat(), 'lng':route.legs[l].via_waypoints[j].lng()},
stopover:false
});
}
}

data.waypoints = w;
});

关于google-maps-api-3 - Gmaps 方向保存路线,航点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11517952/

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