我有一个MapBox JS implmenetation,里面有成千上万个通过GeoJson加载的特征。一个需求是允许用户选择一个标记,并能够更新它的一些数据。我通过处理一个 '点击' 处理程序,并显示一个窗口,允许一些新的输入和一个更新按钮,它调用服务器并返回一个响应。简单的场景。
然而,当改变回调的属性值时,似乎不会在再次点击时显示新数据。我有以下内容 例子 显示我的流程的简化版本,更新发生在第160行。
// When a click event occurs on a feature open a popup at the
// location of the feature, which allows upadting
map.on('click', 'places', function(e) {
var coordinates = e.features[0].geometry.coordinates.slice();
var description = e.features[0].properties.description;
// Ensure that if the map is zoomed out such that multiple
// copies of the feature are visible, the popup appears
// over the copy being pointed to.
while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
}
new mapboxgl.Popup()
.setLngLat(coordinates)
.setHTML(description)
.addTo(map);
// change the description value. this is only a POC.
// real applications will update data on a server and only then update the description.
// is this a copy or a reference to the feature?!?
e.features[0].properties.description = "my new description"; // update happens here - #160
});
我假设在 "点击 "处理程序中返回的功能是在 e.特征 是对原始特征的引用,因此可以很容易地改变,但似乎并不是这样。我看到的唯一的替代方法是从源中查询特征,找到原始特征对象,更新它,并重置源中的数据,但这似乎开销太大。
这里正确的做法是什么?
如果我理解正确的话,你的工作流程是。
第5步你有几个选择。
最简单的是。
map.setData()
再次。