1 //计算线路 2 function calRoute(myOrigin, myDestination, MiddleLocation,MiddleLocation2) { 3 var request = { 4 origin: myOrigin, 5 destination: myDestination, 6 waypoints: [{ location: MiddleLocation, stopover: true }, { location: MiddleLocation2, stopover: true}], 7 optimizeWaypoints:true, 8 travelMode: google.maps.DirectionsTravelMode.DRIVING 9 }; 10 11 directionsService.route(request, function (response, status) { 12 if (status == google.maps.DirectionsStatus.OK) { 13 directionDisplay.setDirections(response); 14 } 15 });
waypoints中的location用数组是不行的,目前我的waypoints是写死的,这样解决不了实际问题,我用了循环也不行,求高手指导,谢谢!
[{ location: MiddleLocation, stopover: true }, { location: MiddleLocation2, stopover: true}],
这是一个数组,你可以在外面定义一个数组,把值都赋值好了,再把数组传递给waypoints
var myMiddleLocation = [{ location: myMiddleLocation1, stopover: true }, { location: myMiddleLocation2, stopover: true}];
我这样 传myMiddleLocation参数是可以的,但是我动态添加 myMiddleLocation.push("[{location:" + myMiddleLocation1 + "," + "stopover:true}");
// myMiddleLocation.push("{location:" + myMiddleLocation2 + "," + "stopover:true}]");不能实现。
waypoints 是写死的什么意思? 你想写活呗?
如果是,我理解为你想添加任意多个经由点(waypoints),那就用动态数组存waypoints的信息吧,如果google 把waypoints定义成了 waypoints[] 形式的数组 那你就用一下 arraylist 转换一下。如果还有什么问题就追问我,并表述再清楚、具体一些。