user1469734
user1469734

Reputation: 801

Mapbox GL JS update Coordinates of places Layer

I'm using Mapbox GL JS and I wanna update Coordinates of a Layer I placed with

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]
                }
            }]
        }
    }
});

And after a while I change the coordinates and I wanna update that on the Map. Except it doesn't work like I do in this code:

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

I can use a Marker(), but I wanna add an "icon-image" which I made with the addImage() and that is only possible with the addLayer().

Upvotes: 1

Views: 1760

Answers (1)

AndrewHarvey
AndrewHarvey

Reputation: 3047

You need to setData on the source not the layer. Since you've used the shorthand approach to create a source as part of addLayer, your source will be named the same as the layer id.

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

Upvotes: 2

Related Questions