gpt4 book ai didi

MapBox 标记在缩放时移动

转载 作者:行者123 更新时间:2023-12-02 05:34:16 60 4
gpt4 key购买 nike

我正在开发 MapBoxgl,我想添加几个标记。

这是我的index.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<link href=" /assets/css/bootstrap.min.css " rel="stylesheet" />
<link href=" /assets/css/mapbox-gl.css " rel="stylesheet" />
<link href=" /assets/css/main.css " rel="stylesheet" />
</head>
<body>
<div id="map"></div>

<script src="/assets/js/mapbox-gl.js"></script>
<script src="/assets/js/map-style.js"></script>

</body>
</html>

这是map-style.js:

var map = new mapboxgl.Map({
container: 'map',
center: [57.3221, 33.5928],
zoom: 5,
style: style
});


var geojson = {
type: 'FeatureCollection',
features: [{
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [30.61, 46.28]
},
properties: {
title: 'point 1',
description: 'point 1 Description',
message: 'point1',
iconSize: [25, 25]
}
},
{
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [30.62, 46.2845]
},
properties: {
title: 'point 2',
description: 'point 2 Description',
message: 'point 2',
iconSize: [25, 25]
}
}]
};

map.on('load', function () {
// add markers to map
geojson.features.forEach(function(marker) {
// create a DOM element for the marker
var el = document.createElement('div');
el.className = 'markers';
el.style.backgroundImage = 'url(assets/marker-azure.png)';
//el.style.width = marker.properties.iconSize[0] + 'px';
el.style.height = marker.properties.iconSize[1] + 'px';

el.addEventListener('click', function() {
window.alert(marker.properties.message);
});

// add marker to map
new mapboxgl.Marker(el)
.setLngLat(marker.geometry.coordinates)
.addTo(map);
});
});

以下是与 map 和标记相关的 main.css 部分:

#map { position:absolute; top:0; bottom:0; width:100% }

.markers {
display: absolute;
border: none;
border-radius: 50%;
cursor: pointer;
padding: 0;
}

所以,我的问题是,当我向标记添加宽度属性时,它们的图标可以正确显示(有点拉伸(stretch)),但它们的坐标错误并在缩放时移动,如下图所示:

enter image description here

另一方面,如果宽度属性被消除,它们就在正确的位置,并且不会在缩放时移动,但它们非常拉伸(stretch),实际上与屏幕一样宽(下图):

enter image description here

值得注意的是我使用了 Bootstrap 。难道是这个原因吗?如果没有,问题出在哪里?

谢谢

最佳答案

import 'mapbox-gl/dist/mapbox-gl.css'; 

添加导入 css 对我有用。

关于MapBox 标记在缩放时移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47047931/

60 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com