Mapbox GL JS更新位置坐标

时间:2019-06-13 08:32:58

标签: mapbox mapbox-gl-js

我正在使用Mapbox GL JS,我想更新放置在

上的图层的坐标
self.map.addLayer({
    "id": "first-location",
    "type": "circle",
    "source": {
        "type": "geojson",
        "data": {
            "type": "FeatureCollection",
            "features": [{
                "type": "Feature",
                "geometry": {
                    "type": "Point",
                    "coordinates": [self.lng, self.lat]
                }
            }]
        }
    }
});

一段时间后,我更改了坐标,并想在地图上进行更新。除非它不像我在这段代码中那样工作:

self.map.getLayer('first-location').setData({
    "type": "FeatureCollection",
    "features": [{
        "type": "Feature",
        "geometry": {
            "type": "Point",
            "coordinates": [self.lng, self.lat]
        }
    }]
});

我可以使用Marker(),但是我想添加一个用"icon-image"制作的addImage(),只有addLayer()才可以。

1 个答案:

答案 0 :(得分:1)

您需要在源而不是图层上设置setData。由于您使用了简写方法来创建源,并将其作为addLayer的一部分,因此您的源将被命名为与图层ID相同。

map.getSource('first-location')。setData(...)