谷歌地图api方向缩放不起作用

时间:2014-09-29 08:15:41

标签: javascript google-maps-api-3

我使用下面的代码来显示两个距离之间的距离但缩放不起作用它始终处于默认缩放级别。我尝试设置很多值,但它不起作用。请帮忙。

var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var from = "";

function initialize() {
  directionsDisplay = new google.maps.DirectionsRenderer();
  directionsDisplay.preserveViewport = false;
  var dallas = new google.maps.LatLng(32.8931567, -97.0402193);

  var mapOptions = {
    zoom: 15,
    center: dallas
  };
  map = new google.maps.Map(document.getElementById('googleMap'), mapOptions);
  map.setZoom(map.getZoom()+5);
  directionsDisplay.setMap(map);
  calcRoute();
}

function calcRoute() {
  var start = 'dallas airport terminal a';
  var end =  'SpringHill Suites';
  console.log("HotelDetail: " + end);
  var request = {
    origin: start,
    destination: end,
    travelMode: google.maps.TravelMode.DRIVING
  };
  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
    }
  });
}

google.maps.event.addDomListener(window, 'load', initialize);

1 个答案:

答案 0 :(得分:0)

对于您想要实现的目标,preserveViewport属性需要像这样设置为true

directionsDisplay = new google.maps.DirectionsRenderer({
    preserveViewport: true
});
//directionsDisplay.preserveViewport = false;

此外,您的代码将缩放级别设置为20,这太高而无法在地图上看到任何内容。这是一个初始缩放级别为12的工作示例:JSFiddle