我是谷歌地图Javascript API的新手,并做了一些基本的事情。我想展示黄石国家公园的一日游路线。起点是West Thumb Geyser Basin,终点是Norris Geyser Basin,下面显示的代码(保存为html文件)。
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script>
function init() {
var map = new google.maps.Map(document.getElementById("googleMap"), {
center: new google.maps.LatLng({lat: 44.427963, lng: -110.5906437}),// Yellowstone National Park
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var directionsService = new google.maps.DirectionsService;
var directionsDisplay = new google.maps.DirectionsRenderer;
directionsDisplay.setMap(map);
calculateAndDisplayRoute(directionsService, directionsDisplay);
}
function calculateAndDisplayRoute(directionsService, directionsDisplay) {
directionsService.route({
origin: {lat: 44.4170394, lng: -110.5740972}, // West Thumb Geyser Basin,
destination: {lat: 44.7262344, lng: -110.7217907}, // Norris Geyser Basin,
travelMode: google.maps.TravelMode.DRIVING,
}, function(response, status) {
if (status === google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
google.maps.event.addDomListener(window, 'load', init);
</script>
</head>
<body>
<font size=2><b>Yellowstone National Parks</b></font>
<div id=googleMap style="width:1100px;height:800px;"></div>
</body>
</html>
但返回的路线不是最佳路线。 Please see the returned route here
似乎是Google Maps JavaScript API错误?有什么想法/想法吗?提前谢谢。
更新了代码。另一件有趣的事。没有optimizeWaypoints,它工作正常。
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script>
function init() {
var map = new google.maps.Map(document.getElementById("googleMap"), {
center: new google.maps.LatLng({lat: 44.427963, lng: -110.5906437}),// Yellowstone National Park
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var directionsService = new google.maps.DirectionsService;
var directionsDisplay = new google.maps.DirectionsRenderer;
directionsDisplay.setMap(map);
calculateAndDisplayRoute(directionsService, directionsDisplay);
}
function calculateAndDisplayRoute(directionsService, directionsDisplay) {
var wayPoints = [{ location: {lat: 44.4604826, lng: -110.8303263}, stopover: true }, { location: {lat: 44.525086, lng: -110.840378}, stopover: true }]; // Old Faithful Geyser and Grand Prismatic Spring
directionsService.route({
origin: {lat: 44.4170394, lng: -110.5740972}, // West Thumb Geyser Basin,
destination: {lat: 44.7262344, lng: -110.7217907}, // Norris Geyser Basin,
waypoints: wayPoints,
//optimizeWaypoints: true,
travelMode: google.maps.TravelMode.DRIVING,
}, function(response, status) {
if (status === google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
google.maps.event.addDomListener(window, 'load', init);
</script>
</head>
<body>
<font size=2><b>Yellowstone National Parks</b></font>
<div id=googleMap style="width:1100px;height:800px;"></div>
</body>
</html>
答案 0 :(得分:0)
它无法优化航点的顺序(您只有一个航点)。 optimizeWaypoints
选项仅重新排列航点,不会更改起点或目的地。
默认情况下,路线服务按给定顺序计算通过提供的路标的路线。或者,您可以在DirectionsRequest中传递optimizeWaypoints:true,以允许路线服务通过以更有效的顺序重新排列路标来优化提供的路线。 (此优化是旅行商问题的一个应用。)所有航路点必须是路线服务的中途停留,以优化其路线。