正如您从我的 Mapbox GL JS map 上的图片中看到的,我试图将“W”标记径向定位在中心的小“2”标记周围,但我不知道如何实现这一点。
您可以查看下面随附的 fiddle 以更好地理解这一点。
但本质上,有一个数组 w_markers_arr
,对于每个数组内容,我向 map 添加一个标记,使用以下代码将其从中心标记偏移一点:
"coordinates": [
center_marker.features[0].geometry.coordinates[0] - (w_markers_arr.length * 0.0006) + (i * 0.002),
center_marker.features[0].geometry.coordinates[1] - 0.001 - (Math.sin(i * 0.001) * 0.5)
]
我尝试过使用 Math.PI
、Math.PI/2
,使其出现在中心标记周围,但没有成功。
在我附加的当前代码中,我尝试使用 Math.Sin()
以某种方式使其出现在波浪中(我丢失了tbh),但它也不成功。
希望有人能指出我正确的方向。非常感谢任何帮助。
#geocoder-container > div {
min-width:50%;
margin-left:25%;
}
.mapboxgl-ctrl-geocoder {
display: none;
}
.mapboxgl-ctrl-logo, .mapboxgl-ctrl-bottom-right {
display: none !important;
}
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Set a point after Geocoder result</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.49.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.49.0/mapbox-gl.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<script src='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v2.3.0/mapbox-gl-geocoder.min.js'></script>
<link rel='stylesheet' href='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v2.3.0/mapbox-gl-geocoder.css' type='text/css' />
<div id='map'></div>
<script>
var center_marker;
mapboxgl.accessToken = 'pk.eyJ1IjoiZmFyaXNrYXNzaW0iLCJhIjoiSk1MaUthdyJ9.vkxtdDbYdLi524WwlKORBw';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/fariskassim/cjmszx78b266o2rlar02ytynj',
center: [127.017613, 37.591672],
maxBounds: [
[126.972368, 37.572532], // Southwest coordinates
[127.073682, 37.629226] // Northeast coordinates
], // Sets bounds as max
zoom: 14.2,
minZoom: 14.2
// pitch: 60, // pitch in degrees
// bearing: -60, // bearing in degrees
});
// After the map style has loaded on the page, add a source layer and default
// styling for a single point.
map.on('load', function() {
map.addSource('single-point', {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": []
}
});
center_marker = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"message": "Marker 0",
},
"geometry": {
"type": "Point",
"coordinates": [127.017613, 37.591672],
}
},
]
};
// create center marker and add to map
var el = document.createElement('div');
el.className = 'marker_places';
el.dataset.index = 0;
el.style.backgroundImage = 'url(http://fariskassim.com/stage/rebel9/seongbukgu/master/v2/img/icn/icn_marker_1.svg)';
el.style.width = '25px';
el.style.height = '25px';
// add center marker to map
new mapboxgl.Marker(el)
.setLngLat(center_marker.features[0].geometry.coordinates)
.addTo(map);
/******/
var w_markers_arr = ["w_marker1", "w_marker2", "w_marker3", "w_marker4", "w_marker5", "w_marker6",]
var w_markers = {
"type": "FeatureCollection",
"features": []
};
// add w markers
for (var i = 0; i < w_markers_arr.length; i++) {
var w_markers_feature_toadd = {
"type": "Feature",
"properties": {
"message": "Work "+i,
"index": i
},
"geometry": {
"type": "Point",
"coordinates": [
center_marker.features[0].geometry.coordinates[0] - (w_markers_arr.length * 0.0006) + (i * 0.002),
center_marker.features[0].geometry.coordinates[1] - 0.001 - (Math.sin(i * 0.001) * 0.5)
]
}
}
w_markers.features.push(w_markers_feature_toadd);
// create marker ele and add to map
var el = document.createElement('div');
el.className = 'marker_floaters';
el.dataset.index = i;
el.style.backgroundImage = 'url(https://www.fariskassim.com/stage/rebel9/seongbukgu/master/v2/img/icn/icn_marker_works.svg)';
el.style.width = '35px';
el.style.height = '35px';
// add marker to map
new mapboxgl.Marker(el)
.setLngLat(w_markers_feature_toadd.geometry.coordinates)
.addTo(map);
}
});
</script>
</body>
</html>
最佳答案
用纬度/经度制作一个圆可能非常棘手。
图书馆Turf.js提供了一些非常有用的工具,例如circle
,它返回一个以中心点和半径为中心的圆:
var radius = 0.2;
var options = { steps: w_markers_arr.length, units: 'kilometers' };
var circleCenter = center_marker.features[0].geometry.coordinates;
var circle = turf.circle(circleCenter, radius, options);
var circleCoordinates = circle.geometry.coordinates[0];
通过将步数设置为标记的数量,circleCooperatives[i]
将包含每个标记的坐标(+ 最后一个与第一个标记相同的坐标) .
关于javascript - Mapbox GL js - 围绕给定坐标径向添加多个标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52760507/