我需要围绕用户点击的点创建一个圆圈。我该怎么做?每个教程都展示了从 geojson 源中提取一个圆而不是创建一个圆。还需要能够编辑半径。
你自己尝试过吗?按照 mapbox 示例,您应该能够了解如何构建类似的东西。
你需要做三件事:
这是我如何编码的一个例子:https : //jsfiddle.net/andi_lo/495t0dx2/
希望能帮到你
mapboxgl.accessToken = '####';
var map = new mapboxgl.Map({
container: 'map', // container id
style: 'mapbox://styles/mapbox/light-v9', //stylesheet location
center: [-74.50, 40], // starting position
zoom: 9 // starting zoom
});
map.on('load', () => {
const points = turf.featureCollection([]);
// add data source to hold our data we want to display
map.addSource('circleData', {
type: 'geojson',
data: {
type: 'FeatureCollection',
features: [],
},
});
// add a layer that displays the data
map.addLayer({
id: 'data',
type: 'circle',
source: 'circleData',
paint: {
'circle-color': '#00b7bf',
'circle-radius': 8,
'circle-stroke-width': 1,
'circle-stroke-color': '#333',
},
});
// on user click, extract the latitude / longitude, update our data source and display it on our map
map.on('click', (clickEvent) => {
const lngLat = new Array(clickEvent.lngLat.lng, clickEvent.lngLat.lat);
points.features.push(turf.point(lngLat));
map.getSource('circleData').setData(points);
});
});Run Code Online (Sandbox Code Playgroud)
#map {
height: 500px;
}Run Code Online (Sandbox Code Playgroud)
<div id="map"></div>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2896 次 |
| 最近记录: |