javascript - 地图覆盖不显示。如何显示叠加层?
问题描述
我试图在地图上显示一个叠加层,但它没有显示它。我从官方文档中复制了这段代码。这里有什么问题。我该如何解决这个问题?一旦分配了叠加层,此代码是否会刷新地图?此代码是否在地图上绘制叠加层?
代码:
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 不是函数
现场演示:
解决方案
您错过了顶部的这一部分:
var overlay;
USGSOverlay.prototype = new google.maps.OverlayView();
这将为您提供来自 OverlayView 的 setMap 和其他功能
推荐阅读
- python - 在 Python 中,我有一个字典,如何在第一个索引上添加一个新键?
- sql - 重命名列 MySQL Workbench MariaDB
- javascript - 需要使用 event.currentTarget 进行 Javascript 计算方面的帮助
- javascript - 从 Fullcalendar 向 PHP 传递参数
- mysql - 连接两个几乎相同的没有空值的 sql 结果行
- swift - swift SceneKit 对象为空
- python - 如何过滤列和绘制一堆文件
- azure - 如何在 ARM 模板中获取 AzureAD 用户 principalId
- android - 当应用程序获得焦点时,何时检查 Android API 29 中的剪贴板内容?
- android - MPAndroidChart - 比例线图