Mapbox GL JS 更新位置图层坐标

Mapbox GL JS update Coordinates of places Layer

我正在使用 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() 才有可能。

您需要在源而不是图层上设置数据。由于您已使用 shorthand 方法创建源作为 addLayer 的一部分,因此您的源将被命名为与图层 ID 相同的名称。

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