是否可以隐藏(切换)主地图图层并仅显示地面叠加图像本身?

时间:2015-07-11 08:42:07

标签: javascript google-maps google-maps-api-3 gmsgroundoverlay

我有一个groundOverlay图像,我希望可以打开/关闭主地图,这样只能显示叠加图像。那可能吗?

PS:我正在使用javascript api v3。

1 个答案:

答案 0 :(得分:1)

您可以实现自定义MapType(不渲染Tiles),然后您只需要在此自定义MapType和内置MapType之间切换(例如ROADMAP):

function initialize() {
  var goo = google.maps,
    map = new goo.Map(document.getElementById('map-canvas'), {
      zoom: 11,
      center: new goo.LatLng(40.740, -74.18),
      mapTypeControl: false

    }),

    mt = function() {
      this.getTile = function() {
        var n = document.createElement('div');
        n.style.cssText = 'background:#fff;width:256px;height:256px;';
        return n;
      };
      this.tileSize = new google.maps.Size(256, 256);
      this.maxZoom = 20;
    },
    //maptype-control
    mtc = document.createElement('div');

  mtc.id = 'mtc';
  map.controls[google.maps.ControlPosition.LEFT_BOTTOM].push(mtc);

  google.maps.event.addDomListener(mtc, 'click', function() {
    console.log(map.getMapTypeId())
    map.setMapTypeId((map.getMapTypeId() === 'hidden') ? google.maps.MapTypeId.ROADMAP : 'hidden');
  });
  google.maps.event.addListener(map, 'maptypeid_changed', function() {
    mtc.style.textDecoration = ((this.getMapTypeId() === 'hidden') ? 'none' : 'line-through');
  });

  map.mapTypes.set('hidden', new mt);
  map.setMapTypeId('hidden');

  new goo.GroundOverlay(
    'https://www.lib.utexas.edu/maps/historical/newark_nj_1922.jpg',
    new goo.LatLngBounds(
      new goo.LatLng(40.712216, -74.22655),
      new goo.LatLng(40.773941, -74.12544)
    ), {
      map: map
    }
  );

}

google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map-canvas {
  height: 100%;
  margin: 0px;
  padding: 0px
}
#mtc {
  border: 1px solid #000;
  background: tomato;
  padding: 2px;
  margin: 4px;
  cursor: pointer;
  font-size: 1.4em;
  font-weight: bold;
  border-radius:2px;
}
#mtc:after {
  content: 'Tiles';
}
<script src="https://maps.googleapis.com/maps/api/js?v=3"></script>
<div id="map-canvas"></div>