riv
riv

Reputation: 119

map.removeSource does not remove source geoJSON points

I'm using mapbox to plot points on a search query. Every time a new query is called, new points should overwrite the old points. I do this by calling map.removeSource('id') followed by map.addSource('id', {type:'geojson', 'data: newData }) on react's componentWillReceiveProps lifecycle method. On the map however, new points are added on top of the old points, instead of being overwritten. Any pointers to what could have happened?

I'm using react for the ui and redux to manage state, and I've checked that new data points are overwriting old data points, which is why I think this is a mapbox problem, or a me problem of not knowing how to use the api correctly.

Upvotes: 1

Views: 958

Answers (1)

Lauren Budorick
Lauren Budorick

Reputation: 666

Without a working example it's hard to say, but rather than removing and re-adding the source (especially if it's sharing an ID, which sounds like a likely race condition) you should use Source#setData to update the data in your existing source object:

map.getSource('id').setData({ type: 'FeatureCollection', features: [...] });

Upvotes: 2

Related Questions