javascript - 当地图处于最大缩放时,地图框弹出位置错误(多个世界副本可见)
问题描述
我有一个 Mapbox 地图的问题,当地图缩小时,附加到标记的弹出窗口显示错误的位置,因此我们看到多个世界副本。请参阅下面的示例。当地图放大并且只有一个世界可见时,弹出窗口会显示在正确的位置。
这是用于添加标记+弹出窗口并显示它们的代码的简化版本:
// Add markers to the map
features.forEach(function (marker: any, i: number) {
const popUpContent = '<div>Sample</div>'
// Create the popup
const popup = new mapboxgl.Popup({ offset: 25 })
.setHTML(popUpContent)
.on('open', function (event: any) {
const activePoi = document.getElementsByClassName(poiId)[0]
activePoi.classList.add('active')
})
.on('close', function () {
const activePoi = document.getElementsByClassName(poiId)[0]
if (activePoi) {
activePoi.classList.remove('active')
}
})
let mark = new mapboxgl.Marker(markerElement)
.setLngLat(marker.geometry.coordinates)
.setPopup(popup)
.addTo(map)
})
问题
我该如何解决这个问题,以便弹出窗口正确显示在它们各自的标记上方,即使多个世界副本可见?
解决方案
您可以使用 Mapbox 的解决方案:https ://docs.mapbox.com/mapbox-gl-js/example/popup-on-click/
// When a click event occurs on a feature in the places layer, open a popup at the
// location of the feature, with description HTML from its properties.
map.on('click', 'places', function(e) {
var coordinates = e.features[0].geometry.coordinates.slice();
var description = e.features[0].properties.description;
// Ensure that if the map is zoomed out such that multiple
// copies of the feature are visible, the popup appears
// over the copy being pointed to.
while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
}
new mapboxgl.Popup()
.setLngLat(coordinates)
.setHTML(description)
.addTo(map);
});
// ...
}
推荐阅读
- webcenter-sites - 更改页面资产定义
- php - Left JOIN 查询不显示数据
- javascript - 如何使用 javascript 将创建的元素存储在 Cookie 或 localStorage 中?
- excel - Excel VBA如何为单独的用户自动保存
- linux - 带错误控制的 shell 并行编程
- angular - Angular 6 - 触发浏览器“保存登录提示”
- mongodb-query - 在 Mongo DB 中,如何获取数据插入的初始日期
- javascript - 如何使用php在每个随机操作数之后生成随机运算符?
- rest - HERE API 根据计算出的路线显示地图
- barcode-scanner - 我的要求是在 power builder 中开发一个应用程序,它接收扫描的条形码并对其进行解码