假设我有 45.6987 和 34.34433 这样的点 现在该点显示为圆圈,我想将该点显示为正方形或方框。
最佳答案
以下是对https://www.mapbox.com/mapbox-gl-js/example/custom-marker-icons/的轻微修改使用自定义 CSS 的地方。
图像被注释掉并使用带颜色的 DIVS。请注意,给定 GEOJSON 的属性包括“图标大小”——这用于“正方形”的宽度和长度。
总而言之,此示例使用 document.createElement 创建您需要的自定义标记。如果图层中有更多内在选项(如正方形等)而不是仅仅用圆圈来指示 map 上的点,那就太好了。
-确保添加您的 token 以使代码正常工作。
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title></title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.32.1/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.32.1/mapbox-gl.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<style>
.divMarker{
display: block;
cursor: pointer;
width: 300px;
background-color: red;
height: 100px;
box-shadow: 10px 10px 5px #888888;
}
</style>
<div id='map'></div>
<script>
mapboxgl.accessToken = '<ADD TOKEN HERE>';
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-v9',
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 = 'divMarker';
// 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, {offset: [-marker.properties.iconSize[0] / 2, -marker.properties.iconSize[1] / 2]})
.setLngLat(marker.geometry.coordinates)
.addTo(map);
});
</script>
</body>
</html>
关于javascript - 我想更改 mapbox 中特定点的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40373158/