我正在构建一个谷歌地图应用程序,我需要在其中提供多个旅行选项的方向,并且我在侧面板中显示方向。
但是,当我选择一个新的方向请求并将其结果传递给面板时,事情就会变得一团糟,请参阅小提琴http://jsfiddle.net/PLrkp/4/
这里是如何重现它首先选择使用的方向过境作为出行方式,然后改成自行车,你会在部分看到奇怪的方框,你可以在其中选择替代路线
注意:这只发生在 Directions 请求对象中的 'provideRouteAlternatives' 设置为 true 时,并且仅当实际上有多个路线时,(看起来该部分没有完全更新)
这是我的代码
<div class="mapsection" >
<form onsubmit="calcRoute();return false;">
<select id="travelmode">
<option value="DRIVING" selected="selected" data-shortMode="c">DRIVING</option>
<option value="TRANSIT" data-shortMode="r" selected>TRANSIT</option>
<option value="WALKING" data-shortMode="w">WALKING</option>
<option value="BICYCLING" data-shortMode="b">BICYCLING</option>
</select>
<input type="text" id="start" value="Washington"/>
<input type="text" id="end" value="New York"/>
<button type="submit">GET DIRECTIONS</button>
</form>
<div id="map_canvas" style="width:100%; height:550px;float:left;"></div>
<div id="directionsPanel" style="float:right;max-width:395px; overflow:scroll;height: 505px;overflow-x: hidden;"></div>
</div>
<script>
//define one global Object
var myMap = {}
//init
function initialize(){
//set up map options
var mapOptions = {
center: new google.maps.LatLng(40.75377,-73.990531),
zoom: 4,
mapTypeId: google.maps.MapTypeId.ROADMAP,
};
myMap.map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
myMap.directionsService = new google.maps.DirectionsService();
myMap.directionsDisplay = new google.maps.DirectionsRenderer();
myMap.directionsDisplay.setMap(myMap.map);
myMap.directionsDisplay.setPanel(document.getElementById("directionsPanel"));
}//end init
//directions
var calcRoute = function() {
var start = document.getElementById("start").value,
end = document.getElementById("end").value,
request = {
origin:start,
destination:end,
durationInTraffic :true,
transitOptions: {
departureTime: new Date()
},
provideRouteAlternatives : true,
travelMode: document.getElementById("travelmode").value
};
myMap.directionsService.route(request, function(result, status) {
if(status == google.maps.DirectionsStatus.OK) {
myMap.directionsDisplay.setDirections(result);
}else{
alert("something went wrong!");
}
});
}
//script loader
var loadScript = function() {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY_HERE&sensor=false&callback=initialize";
document.body.appendChild(script);
}
window.onload = loadScript;
</script>