我是 Java Script 新手,有一个简单的问题:我使用 Googlemaps API (TransitLayer) 进行路由。路由工作,方向面板创建成功,但不幸的是蓝色路由线没有出现在地图上。这可能与我对地图的异步初始化有关。(回调函数)见下面我的 javascript 函数,这里是我页面的链接: 我的网站(标签 2:路由)。
任何帮助是极大的赞赏。
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
routingcounter = 0;
var routingmap;
var mapOptions;
function initialize_routing() {
directionsDisplay = new google.maps.DirectionsRenderer();
mapOptions = {
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: new google.maps.LatLng(51.510799, -0.134332)
};
routingmap = new google.maps.Map(document.getElementById('mycanvas'),
mapOptions);
directionsDisplay.setMap(routingmap);
directionsDisplay.setPanel(document.getElementById('directions-panel'));
var transitLayer = new google.maps.TransitLayer();
transitLayer.setMap(routingmap);
}
function loadScript() {
if(routingcounter == 0)
{
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://maps.googleapis.com/maps/api/js?sensor=false&callback=initialize_routing";
document.body.appendChild(script);
routingcounter++;
}
}
function compute_route()
{
var start = $('from').value;
var over = $('over').value;
var waypts = [];
var end = $('to').value;
if ($('from').value == "" || $('to').value=="")
{
alert("Fill in From- and To-Textfields! Try again!");
return;
}
start += ",London"
end += ",London"
if($('over').value == "")
{
var request = {
origin: start,
destination: end,
travelMode: google.maps.TravelMode.TRANSIT
};
}
else
{
over += ",London"
waypts.push({
location:over,
stopover:true
});
var request = {
origin: start,
destination: end,
waypoints: waypts,
optimizeWaypoints: true,
travelMode: google.maps.TravelMode.TRANSIT
};
}
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK)
{
directionsDisplay.setDirections(response);
}
else
{
alert("No matching places found. Retry please!");
$('from').value == "";
$('over').value == "";
$('to').value == "";
}
});
}
google.maps.event.addDomListener(window, 'load', initialize_routing);