Search code examples
xamarinmapboxmapbox-android

New LineLayer won't appear unless map zoomed in/out


I have some routes in my code, and user can choose which one they wanna see on their map. For example, there are route X and route Y. Then user is seeing route Y and tap a button to see route X. Route Y is hidden and route X is shown.

Now here's my code to show route X (same for route Y, except different properties)

FeatureCollection featureCollection = BaseFeatureCollection.FromJson
     ("{\"type\":\"FeatureCollection\",\"features\":" + 
     "[{\"type\":\"Feature\",\"geometry\":{\"type\":\"LineString\"" +
     ",\"coordinates\":[" + stringCoordinates + "]},\"properties\":{}}]}");
GeoJsonSource geoJsonSource = new GeoJsonSource("route", featureCollection);
_mapboxMap.AddSource(geoJsonSource);

_lineLayerX = new LineLayer("linelayerred", "route");
_lineLayerX.SetProperties(
  PropertyFactory.LineWidth(new Java.Lang.Float(8.0f)),
  PropertyFactory.LinePattern("routex")
);

if (isShowRouteX) {
    _mapboxMap.AddLayer(_lineLayerX);
}

Then when I wan't to hide and show another route

_mapboxMap.RemoveLayer(_lineLayerX);
_mapboxMap.AddLayer(_lineLayerY);

Code simplified, but basically that's all I do, and it does shows the route.

Now, the first time I add LineLayer, it would be shown immediately on map, I don't need to zoom in or out. But if new LineLayer added (after removing the old one), changes won't appear unless map is zoomed in or out, as if I have to do that to trigger redraw. Not even panning map to any direction would make new line appear. RemoveLayer executed flawlessly though.

I've even tried _mapView.Invalidate(), still doesn't work. What should I add so that user won't need to zoom in/out to see new LayerLine?

Also, I know that PropertyFactory has Visibility and it should/might work better for my problem, yet it doesn't work, especially Property.None. And not sure if it's still on topic in this questions, so I won't go further into it.


Solution

  • Finally found it, apparently I lacked this line

    _mapboxMap.RemoveSource(geoJsonSource);
    

    I put it on

    _mapboxMap.RemoveLayer(_lineLayerX);
    _mapboxMap.RemoveSource(geoJsonSource);
    _mapboxMap.AddLayer(_lineLayerY);
    

    and now the new line appears without the needs to zoom in or out.