谷歌地图V3 - 地图确定,方向不是

时间:2012-12-25 00:38:15

标签: google-maps-api-3

这是页面: my site 如您所见,地图显示OK,居中和缩放。问题是方向没有显示......

Google地图代码:

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 个答案:

答案 0 :(得分:2)

您至少有两个问题:

  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);
         }
       });
     }