在Google地图中将地面覆盖可见性限制为特定缩放级别?

时间:2016-03-18 23:21:17

标签: javascript google-maps zoom overlay

有没有办法将Google地图中地面叠加层的可见性限制为仅限于12到14之间的某些缩放级别。

Google's example中显示的official documentation中,地面覆盖显示所有缩放级别。 这是他们的代码:

var historicalOverlay;

function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 13,
    center: {lat: 40.740, lng: -74.18}
  });

  var imageBounds = {
    north: 40.773941,
    south: 40.712216,
    east: -74.12544,
    west: -74.22655
  };

  historicalOverlay = new google.maps.GroundOverlay(
      'https://www.lib.utexas.edu/maps/historical/newark_nj_1922.jpg',
      imageBounds);
  historicalOverlay.setMap(map);
}

1 个答案:

答案 0 :(得分:0)

map on the 'zoom_changed` event (documentation)添加事件监听器:

  

zoom_changed |参数:无   地图缩放属性更改时会触发此事件。

google.maps.event.addListener(map, 'zoom_changed', function() {
  var zoom = map.getZoom();
  if ((zoom > 12) && (zoom < 15)) {
    historicalOverlay.setMap(map);
  } else {
    historicalOverlay.setMap(null);
  }
});

&#13;
&#13;
var historicalOverlay;
var map;

function initMap() {
  map = new google.maps.Map(document.getElementById('map'), {
    zoom: 13,
    center: {
      lat: 40.740,
      lng: -74.18
    }
  });
  google.maps.event.addListener(map, 'zoom_changed', function() {
    document.getElementById('zoom').innerHTML = "zoom =" + map.getZoom();
    var zoom = map.getZoom();
    if ((zoom > 12) && (zoom < 15)) {
      historicalOverlay.setMap(map);
    } else {
      historicalOverlay.setMap(null);
    }
  })

  var imageBounds = {
    north: 40.773941,
    south: 40.712216,
    east: -74.12544,
    west: -74.22655
  };

  historicalOverlay = new google.maps.GroundOverlay(
    'https://www.lib.utexas.edu/maps/historical/newark_nj_1922.jpg',
    imageBounds);
  historicalOverlay.setMap(map);
}
google.maps.event.addDomListener(window, "load", initMap);
&#13;
html,
body,
#map {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
&#13;
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="zoom"></div>
<div id="map"></div>
&#13;
&#13;
&#13;