编辑标记大小和弹出框大小

Edit marker size and popup box size

我设法让标记和弹出框出现在 Mapbox 中 API
但是,我似乎无法编辑它们的大小和弹出窗口大小。

for (let i = 0; i < locations.length; i++) {
  let location = locations[i];

  let marker = new mapboxgl.Marker({ color: '#FF8C00' });

  marker.setLngLat(location.coordinates);

  let popup = new mapboxgl.Popup({ offset: 40 });
  popup.setHTML(location.description);

  marker.setPopup(popup);

  // Display the marker.
  marker.addTo(map);

  // Display the popup.
  popup.addTo(map);
}

Mapbox 通常建议使用 Annotation Plugin 创建标记并与之交互。

但是,如果您想坚持使用您提供的示例,可以遵循此 Mapbox example,其中自定义图标用于标记符号。这些图标的大小可以通过 iconSize 属性:

进行调整

<script>
 mapboxgl.accessToken = 'access_token';
var geojson = {
'type': 'FeatureCollection',
'features': [
{
'type': 'Feature',
'properties': {
'message': 'Foo',
'iconSize': [60, 60]
},
'geometry': {
'type': 'Point',
'coordinates': [-66.324462890625, -16.024695711685304]
}
},
{
'type': 'Feature',
'properties': {
'message': 'Bar',
'iconSize': [50, 50]
},
'geometry': {
'type': 'Point',
'coordinates': [-61.2158203125, -15.97189158092897]
}
},
{
'type': 'Feature',
'properties': {
'message': 'Baz',
'iconSize': [40, 40]
},
'geometry': {
'type': 'Point',
'coordinates': [-63.29223632812499, -18.28151823530889]
}
}
]
};
 
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v11',
center: [-65.017, -16.457],
zoom: 5
});
 
// add markers to map
geojson.features.forEach(function(marker) {
// create a DOM element for the marker
var el = document.createElement('div');
el.className = 'marker';
el.style.backgroundImage =
'url(https://placekitten.com/g/' +
marker.properties.iconSize.join('/') +
'/)';
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);
});
</script>