地图叠加层未显示。如何显示叠加层?

时间:2019-02-01 06:03:49

标签: javascript html

我正在尝试在地图上显示叠加层,但未显示出来。我从官方文档中复制了此代码。这怎么了我该如何解决这个问题?分配叠加层后,此代码是否会刷新地图?这段代码是否在地图上绘制了叠加层?

代码:

var overlay;
function initMap() {
  USGSOverlay.prototype = new google.maps.OverlayView();
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 20,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        zoomControl: false,
        mapTypeControl: false,
        scaleControl: false,
        panControl: false,
        navigationControl: false,
        streetViewControl: false,
        gestureHandling: 'cooperative',
  });

  var bounds = new google.maps.LatLngBounds(
          new google.maps.LatLng(62.281819, -150.287132),
          new google.maps.LatLng(62.400471, -150.005608)
      );

      var srcImage = 'https://developers.google.com/maps/documentation/' + 'javascript/examples/full/images/talkeetna.png';

      overlay = new USGSOverlay(bounds, srcImage, map);
}

function USGSOverlay(bounds, image, map) {
  this.bounds_ = bounds;
      this.image_ = image;
      this.map_ = map;

      this.div_ = null;

      this.setMap(map);
}

USGSOverlay.prototype.onAdd = function() {
  var div = document.createElement('div');
      div.style.borderStyle = 'none';
      div.style.borderWidth = '0px';
      div.style.position = 'absolute';

      var img = document.createElement('img');
      img.src = this.image_;
      img.style.width = '100%';
      img.style.height = '100%';
      img.style.position = 'absolute';
      div.appendChild(img);

      this.div_ = div;

      var panes = this.getPanes();
      panes.overlayLayer.appendChild(div);
}

USGSOverlay.prototype.draw = function() {
  var overlayProjection = this.getProjection();
  var sw = overlayProjection.fromLatLngToDivPixel(this.bounds_.getSouthWest());
      var ne = overlayProjection.fromLatLngToDivPixel(this.bounds_.getNorthEast());
      var div = this.div_;
      div.style.left = sw.x + 'px';
      div.style.top = ne.y + 'px';
      div.style.width = (ne.x - sw.x) + 'px';
      div.style.height = (sw.y - ne.y) + 'px';
}

USGSOverlay.prototype.onRemove = function() {
  this.div_.parentNode.removeChild(this.div_);
      this.div_ = null;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/gmaps.js/0.4.25/gmaps.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="map" data-map-zoom="9"></div>

错误:

  

this.setMap不是函数

现场演示:

https://www.vsss.co.in/Right/

1 个答案:

答案 0 :(得分:0)

您错过了顶部的这一部分:

      var overlay;
      USGSOverlay.prototype = new google.maps.OverlayView();

这将为您提供setMap和OverlayView中的其他功能