最近,当在我们的应用程序的起点和终点位置之间使用带有“航路点”的 DirectionsService 时,我们一直在返回这些在视觉上没有意义的“之字形”路线。
看起来,一切都很好,但在第 7 个航路点之后的某个地方,该路线会以戏剧性的锯齿形返回更早的航路点,然后似乎试图将自己纠正回正确的路径。
我附上了一张图片来展示我想说的内容,并指出了事情出错的地方。此外,我在下面包含了示例代码,并在此处发布了该问题的工作演示:
http://dev.anytraq.com:8080/test.html(工作版)
我想知道这里是否有人遇到过这样的问题,或者可以阐明可能发生的事情?正如您在下面看到的,代码非常简单......基本上我们只定义了一个起点和终点 LatLng 对象,以及它们之间的 8 个航路点,然后将其发送到 DirectionsService。
我会很感激任何可能想到的建议,我完全被难住了。
<!DOCTYPE html>
<html>
<head>
<title>ZigZagged Directions Example</title>
<meta charset="UTF-8">
<style type="text/css">
html, body, #map_canvas {
margin: 0;
padding: 0;
height: 100%;
}
</style>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var map;
function initialize() {
// map options
var myOptions = {
zoom: 8,
center: new google.maps.LatLng(-34.397, 150.644),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
// the map
map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);
// directions service
directionsService = new google.maps.DirectionsService();
// Define our origin position, the start of our trip.
originPosition = new google.maps.LatLng('37.66258', '-121.87058');
// Define 8 waypoints to place between the origin and the destination
waypoint1 = new google.maps.LatLng('37.65638', '-121.87750');
waypoint2 = new google.maps.LatLng('37.64333', '-121.88174');
waypoint3 = new google.maps.LatLng('37.63676', '-121.88447');
waypoint4 = new google.maps.LatLng('37.60746', '-121.87343');
waypoint5 = new google.maps.LatLng('37.57868', '-121.87702');
waypoint6 = new google.maps.LatLng('37.56368', '-121.90382');
waypoint7 = new google.maps.LatLng('37.54175', '-121.92334');
waypoint8 = new google.maps.LatLng('37.51806', '-121.94201');
// push the waypoints defined above into an array
wayPointArray = new Array();
wayPointArray.push({location: waypoint1, stopover: false});
wayPointArray.push({location: waypoint2, stopover: false});
wayPointArray.push({location: waypoint3, stopover: false});
wayPointArray.push({location: waypoint4, stopover: false});
wayPointArray.push({location: waypoint5, stopover: false});
wayPointArray.push({location: waypoint6, stopover: false});
wayPointArray.push({location: waypoint7, stopover: false});
wayPointArray.push({location: waypoint8 , stopover: false});
// define our definition position, the last stop of our trip
destinationPosition = new google.maps.LatLng('37.50162', '-121.93034');
var request = {
origin: originPosition,
destination: destinationPosition,
waypoints: wayPointArray,
travelMode: google.maps.DirectionsTravelMode.DRIVING,
unitSystem: google.maps.DirectionsUnitSystem.METRIC,
optimizeWaypoints: false
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
var polyOpts = {
strokeOpacity: 1.0,
strokeColor: '#0000FF',
strokeWeight: 2
}
var directionsDisplayOptions = {
suppressMarkers: false,
suppressInfoWindows: false,
preserveViewport: false,
polylineOptions: polyOpts
};
directionsRenderer = new google.maps.DirectionsRenderer(directionsDisplayOptions);
directionsRenderer.setMap(map);
directionsRenderer.setDirections(response);
} else {
console.info('could not get route');
console.info(response);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map_canvas"></div>
</body>
</html>