google-maps-api-3 - Google map V3 - map 可以,方向不行

标签 google-maps-api-3

这是页面: my site 正如您所看到的, map 显示正常,居中并按我想要的方式缩放。问题是方向没有显示...

Google map 代码:

var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var myLatlng = new google.maps.LatLng(37.651429,22.267043);    
var myOptions = {
  zoom:9,
  center: new google.maps.LatLng(37.722392,22.769925),
  mapTypeId: google.maps.MapTypeId.ROADMAP,

}
map = new google.maps.Map(document.getElementById("map1"), myOptions);
directionsDisplay.setMap(map);
}
function calcRoute() {
var request = {
    origin:"athens, greece", 
    destination:myLatlng,
    travelMode: google.maps.DirectionsTravelMode.DRIVING,
};
directionsService.route(request, function(response, status) {
  if (status == google.maps.DirectionsStatus.OK) {
    directionsDisplay.setDirections(response);
  }
});
}

有什么建议吗?

最佳答案

您至少有两个问题:

  1. 您有一个 calcRoute 函数来计算和显示路线,但您从未调用它(解决方案:调用它,可能在初始化函数的末尾)。
  2. myLatLng 变量是初始化例程的本地变量,因此当 calcRoute 尝试访问它时不可用(解决方案:将其设为全局变量)。

     var directionDisplay;
     var directionsService = new google.maps.DirectionsService();
     var map;
     var myLatlng = new google.maps.LatLng(37.651429,22.267043);    
    
     function initialize() {
       directionsDisplay = new google.maps.DirectionsRenderer();
       var myOptions = {
         zoom:9,
         center: new google.maps.LatLng(37.722392,22.769925),
         mapTypeId: google.maps.MapTypeId.ROADMAP,
       }
       map = new google.maps.Map(document.getElementById("map1"), myOptions);
       directionsDisplay.setMap(map);
       calcRoute();
     }
    
     function calcRoute() {
       var request = {
         origin:"athens, greece", 
         destination:myLatlng,
         travelMode: google.maps.DirectionsTravelMode.DRIVING,
       };
       directionsService.route(request, function(response, status) {
         if (status == google.maps.DirectionsStatus.OK) {
           directionsDisplay.setDirections(response);
         }
       });
     }
    

关于google-maps-api-3 - Google map V3 - map 可以,方向不行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14026787/

相关文章:

python - 带有 API key 的 PyGeocoder

javascript - Google map 'heatmap' 可以处理密度和数据值吗?

angularjs - Google map 标记和 AngularJS

javascript - ionic : click event taking time for selecting places from Google Places api

javascript - 如何从另一个范围内的函数调用事件监听器?

javascript - Google Maps API js3 未加载整个 map

javascript - 谷歌地图标记不显示

google-maps - 在哪里检索 photo_reference 值以发出 Google Places Photos 请求?

javascript - 为什么谷歌地图不能与 gmaps.js + ie9 一起使用?

javascript - 如何在每个纬度/经度点上添加 2 个带有标记的谷歌地图?