我有通过 GeoJson 加载的数千个功能的 MapBox JS 实现。一项要求是允许用户选择一个标记并能够更新其上的一些数据。我通过处理一个“点击”处理程序并显示一个允许一些新输入的窗口和一个更新按钮来实现这一点,该按钮调用服务器并返回响应。简单的场景。
但是,当更改回调的属性值时,它似乎不会在再次单击时显示新数据。我有以下示例来显示我的过程的简化版本。更新发生在第 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.features下的“点击”处理程序上返回的功能是对原始功能的引用,因此可以轻松更改,但似乎并非如此。我看到的唯一替代方法是从源中查询特征、查找原始特征对象、更新它并重置源中的数据,但这似乎开销太大。
这里的正确方法是什么?