4

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.

pouyan
  • 3,445
  • 4
  • 26
  • 44

2 Answers2

2

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.

0

In my case i had change only GeoJsonSource id, but forgot to change layer and image id. It was solve my problem.

Max Shwed
  • 232
  • 2
  • 10