MapBox标记继续缩放

Mah*_*hdi 7 marker mapbox mapbox-gl-js

我正在使用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>
Run Code Online (Sandbox Code Playgroud)

这是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);
});
});
Run Code Online (Sandbox Code Playgroud)

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

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

.markers {
    display: absolute;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    padding: 0;
}
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

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

在此输入图像描述

值得注意的是我使用过bootstrap.可能是这个原因吗?如果没有,问题是什么?

谢谢

小智 25

import 'mapbox-gl/dist/mapbox-gl.css'; 
Run Code Online (Sandbox Code Playgroud)

添加 import css 对我有用。


Mah*_*hdi 6

我找到了解决方案并在此与遇到此问题的其他人分享。由于使用了不是最新版本的库而导致的问题。升级到最新版本后,我可以摆脱这个问题。

请注意,当您使用 npm 时,有时会出现此类问题。确保库已完全下载并且是最新版本。

可以在此处找到 MapBox 的最新版本。