Javascript 谷歌地图方向API-点击按钮显示备选路线
如何在点击按钮时更改谷歌地图上当前显示的路线?目前,我只能显示正在返回的主路由。我还能够遍历API返回的所有路由。我就是找不到在我拥有的路线之间改变显示的方法。以下是我到目前为止的情况Javascript 谷歌地图方向API-点击按钮显示备选路线,javascript,google-maps-api-3,google-directions-api,Javascript,Google Maps Api 3,Google Directions Api,如何在点击按钮时更改谷歌地图上当前显示的路线?目前,我只能显示正在返回的主路由。我还能够遍历API返回的所有路由。我就是找不到在我拥有的路线之间改变显示的方法。以下是我到目前为止的情况 <!DOCTYPE html> <html> <head> <title>Google Maps API Implementation</title> <script src="http://maps.googleapis.com/
<!DOCTYPE html>
<html>
<head>
<title>Google Maps API Implementation</title>
<script src="http://maps.googleapis.com/maps/api/js"></script>
<script>
var directionsDisplay = new google.maps.DirectionsRenderer();
var directionsService = new google.maps.DirectionsService();
var validRoutes = [];
function initialize() {
// ----- MAP SETUP ----- //
var pierLocation = new google.maps.LatLng(14.6023936,120.9591289);
var destinationLat = 14.60540879665969; // Sample data only
var destinationLong = 120.9795238; // Sample data only
var destinationLocation = new google.maps.LatLng(destinationLat,destinationLong);
var mapProperties = {
center:pierLocation,
zoom:15,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("googleMap"),mapProperties);
directionsDisplay.setMap(map);
calculateRoutes(pierLocation, destinationLocation);
}
function calculateRoutes(pierLocation, destinationLocation) {
var request = {
origin: pierLocation,
destination: destinationLocation,
travelMode: 'DRIVING',
provideRouteAlternatives: true
};
directionsService.route(request, function(result, status) {
if (status === 'OK') {
directionsDisplay.setDirections(result);
var summaryPanel = document.getElementById('directions-panel');
summaryPanel.innerHTML = '';
for (var x=0; x<result.routes.length; x++) {
new google.maps.DirectionsRenderer({
map: googleMap,
directions: result,
routeIndex: x
});
summaryPanel.innerHTML += '<hr><br><b> Route ' + (x+1) + ':<br>';
var route = result.routes[x];
for (var y=0; y<route.legs.length; y++) {
var routeSegment = y + 1;
summaryPanel.innerHTML += route.legs[y].start_address + ' to ';
summaryPanel.innerHTML += route.legs[y].end_address + '<br>';
summaryPanel.innerHTML += route.legs[y].distance.text + '<br><br>';
var steps = route.legs[y].steps;
for (var z=0; z<steps.length; z++) {
var nextSegment = steps[z].path;
summaryPanel.innerHTML += "<li>" + steps[z].instructions;
var dist_dur = "";
if (steps[z].distance && steps[z].distance.text) dist_dur += steps[z].distance.text;
if (steps[z].duration && steps[z].duration.text) dist_dur += " "+steps[z].duration.text;
if (dist_dur != "") {
summaryPanel.innerHTML += "("+dist_dur+")<br /></li>";
} else {
summaryPanel.innerHTML += "</li>";
}
}
summaryPanel.innerHTML += "<br>";
}
}
}
else {
window.alert('Directions request failed due to ' + status);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="googleMap" style="width:500px;height:380px;"></div><br>
<label id="directions-panel"></label>
</body>
</html>
googlemapsapi实现
var directionsDisplay=new google.maps.DirectionsRenderer();
var directionsService=new google.maps.directionsService();
var有效期=[];
函数初始化(){
//-----地图设置------//
var pierLocation=new google.maps.LatLng(14.6023936120.9591289);
var destinationLat=14.60540879665969;//仅示例数据
var destinationLong=120.9795238;//仅示例数据
var destinationLocation=new google.maps.LatLng(destinationLat,destinationLong);
变量映射属性={
中心:pierLocation,
缩放:15,
mapTypeId:google.maps.mapTypeId.ROADMAP
};
var map=new google.maps.map(document.getElementById(“googleMap”),mapProperties);
方向显示.setMap(地图);
计算器输出(pierLocation、destinationLocation);
}
函数计算器输出(pierLocation,destinationLocation){
var请求={
产地:皮尔位置,
目的地:目的地,
travelMode:‘驾驶’,
ProviderRouteAlternatives:true
};
路由(请求、功能(结果、状态){
如果(状态=='OK'){
方向显示。设置方向(结果);
var summaryPanel=document.getElementById('directions-panel');
summaryPanel.innerHTML='';
对于(var x=0;x使用DirectionsDisplay.setRouteIndex()
)
使用方向显示.setRouteIndex()
<input id="btn1" type="button" value="route1" onclick="directionsDisplay.setRouteIndex(0);" />
<input id="btn2" type="button" value="route2" onclick="directionsDisplay.setRouteIndex(1);" />
<input id="btn3" type="button" value="route3" onclick="directionsDisplay.setRouteIndex(2);" />