Search code examples
androidmapbox-android

CannotAddSourceException with Mapbox on Android


I am getting an exception when I try to add a source to a Mapbox map on Android even if I delete every source with the same name before.

I have tries to delete source then layer or layer then source and nothing worked.

    if (map != null){
        String layerName = floor.getLayerName();

        map.removeLayer(layerName);
        //Remove source and layer if already selected
        map.removeSource(SOURCE); //SOURCE IS A CONST
        map.clear();

        String url = GEOSERVER_URL_NAME;
        RasterSource resource = new RasterSource(SOURCE,new TileSet("tileset",url+layerName));
        map.addSource(resource);
        RasterLayer webMapLayer = new RasterLayer(layerName,SOURCE);
        map.addLayerBelow(webMapLayer, "com.mapbox.annotations.points");
        poisViewModel.refreshPoisByName(building.getId(), floor.getId(), "");
    }

I am getting:

com.mapbox.mapboxsdk.style.sources.CannotAddSourceException: Source floor_map_source already exists. floor_map_source is the value of SOURCE.


Solution

  • I found the issue, Actually, the layerName I was using was different and that prevent source deletion. I had to add the name of the old layer and delete it before setting the new layer.