我正在使用mapbox绘制标记和簇,为此我正在使用querySourceFeatures
(https://docs.mapbox.com/mapbox-gl-js/api/#map#querysourcefeatures)。
我将我的设置基于Mapbox(https://docs.mapbox.com/mapbox-gl-js/example/cluster-html/)的博客文章
但是在更新源(使用setData
后)querySourceFeatures
没有更新,因此我看不到新数据显示在地图上。
这是我的代码,经过简化:
const createData = ( ) => {
markersToGenerate += 10;
const totalFeatures = [{ "type": "Feature", "properties": { "id": "ak16994521", "mag": 2.3, "time": 1507425650893, "felt": null, "tsunami": 0 }, "geometry": { "type": "Point", "coordinates": [ -151.5129, 63.1016, 0.0 ] } },
/** repeat this for 1200 times **/
];
const splicedFeatures = totalFeatures.splice(0, markersToGenerate);
return {
'type': 'geojson',
'data': {
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": splicedFeatures
},
'cluster': true,
'clusterRadius': 80,
'clusterProperties': {
// keep separate counts for each magnitude category in a cluster
'mag1': ['+', ['case', mag1, 1, 0]],
'mag2': ['+', ['case', mag2, 1, 0]],
'mag3': ['+', ['case', mag3, 1, 0]],
'mag4': ['+', ['case', mag4, 1, 0]],
'mag5': ['+', ['case', mag5, 1, 0]]
}
}
}
map.on('load', function() {
// add a clustered GeoJSON source for a sample set of earthquakes
map.addSource('earthquakes', createData());
// circle and symbol layers for rendering individual earthquakes (unclustered points)
map.addLayer({
'id': 'earthquake_circle',
'type': 'circle',
'source': 'earthquakes',
'filter': ['!=', 'cluster', true],
'paint': {
'circle-color': [
'case',
mag1,
colors[0],
mag2,
colors[1],
mag3,
colors[2],
mag4,
colors[3],
colors[4]
],
'circle-opacity': 0.6,
'circle-radius': 12
}
});
map.addLayer({
'id': 'earthquake_label',
'type': 'symbol',
'source': 'earthquakes',
'filter': ['!=', 'cluster', true],
'layout': {
'text-field': [
'number-format',
['get', 'mag'],
{ 'min-fraction-digits': 1, 'max-fraction-digits': 1 }
],
'text-font': ['Open Sans Semibold', 'Arial Unicode MS Bold'],
'text-size': 10
},
'paint': {
'text-color': [
'case',
['<', ['get', 'mag'], 3],
'black',
'white'
]
}
});
// every 5 seconds add a marker to the source and set that
setInterval( () => {
console.log('Update source');
map.getSource('earthquakes').setData(createData());
}, 5000)
// objects for caching and keeping track of HTML marker objects (for performance)
var markers = {};
var markersOnScreen = {};
function updateMarkers() {
console.log("# updateMarkers called")
var features = map.querySourceFeatures('earthquakes');
// There are more features retrieved - since some are displayed on multiple tiles ... so they are counted double. The point here is to show the count isn't increasing.
console.log("Total features displayed = " + features.reduce( (totalCount, feature) => {
if (feature.properties.cluster) {
return totalCount + feature.properties.point_count;
} else {
return totalCount + 1;
}
}, 0));
}
有关完整(实际运行)的代码,请检查此代码笔:https://codepen.io/skarnl/pen/RwWrdPm?editors=0010
一些额外的解释,我如何进行测试:
我添加了一个名为createData
的方法,该方法将生成一个新的GeoJson并将其用于setData
调用。我还添加了setInterval
,每5秒添加10个标记。
我可以进行此工作的唯一方法是删除源的[[both 和层,然后再次重新添加它们。这可以...但是由于完全删除了图层,因此会导致闪烁。
我在这里做错了什么?setData
仅需要获取GeoJSON的数据部分-而不是全部的GeoJson对象再来一次。因此,仅设置数据将解决此问题。希望其他人可以使用此信息。
并使用我自己的示例:
map.getSource('earthquakes').setData(createData().data);
// The .data part is important here!
// We don't want to use the complete GeoJSON, but only the data part in it