如何更新MapBox中的单个GeoJson特征属性并实时反映?

问题描述 投票:0回答:1

我有一个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.特征 是对原始特征的引用,因此可以很容易地改变,但似乎并不是这样。我看到的唯一的替代方法是从源中查询特征,找到原始特征对象,更新它,并重置源中的数据,但这似乎开销太大。

这里正确的做法是什么?

javascript geojson mapbox-gl-js mapbox-gl mapbox-marker
1个回答
0
投票

AFAIK & 根据这个 github问题目前,它仍然不能自动&有选择的差异检查你的功能,除非你手动这样做。

其他 演示品setData 在每一个新的(或者,在你的情况下,更新的)功能到来的时候,你都会完全地进行数据重载。

你是已经遇到了数据重载导致的减速jank,还是刚要实施?


0
投票

如果我理解正确的话,你的工作流程是。

  1. 从服务器加载GeoJSON
  2. 将GeoJSON添加到地图中
  3. 用户导致服务器更新
  4. 从服务器加载部分GeoJSON?
  5. ...什么...
  6. 地图现在显示最新的GeoJSON格式

第5步你有几个选择。

最简单的是。

  • 在第一步中,保留一份GeoJSON的副本。
  • 在步骤4中,用新的信息更新该副本(即更新被触及的各个特征的属性)。
  • 在步骤5中,调用 map.setData() 再次。
© www.soinside.com 2019 - 2024. All rights reserved.