Search code examples
androidgeojsonmapbox-android

Switch GeoJsonSource data in MapBox


I want to replace the data behind an existing source. This is trivial in javascript:

map.getSource('trace').setData(data);

See https://docs.mapbox.com/mapbox-gl-js/example/live-update-feature/

That method does not exist in Android. Once I have set a value, I can't replace it (nothing changes if I do):

GeoJsonSource mySource;
mySource.setGeoJson("some json data");

To delete and readd the source, I'd have to remove the layer using the source first: https://github.com/mapbox/mapbox-gl-native/issues/12526

Since I'm getting a fully functioning Style object from the server, removing, recreating and readding the layer/source at the correct position is tedious to say the least.


Solution

  • Above code actually works, but to reset my layer I used:

    mySource.setGeoJson("");
    

    Setting the source to empty String breaks something. Afterwards the GeoJson can not be set to something meaningful again. So to reset data, I'll have to use the visibility attribute of a layer. Replacing meaningful GeoJson with meaningful GeoJson works just fine.