Custom TileProvider with Polygon in Android maps

2019-07-25 07:13发布

问题:

I'm having problems drawing a polygon on Android map. The problem is that I have TileOverlays so the polygon is kinda under the overlays.

I'm drawing a polygon on GoogleMap like this:

map.addPolygon(new PolygonOptions().add(points).strokeColor(Color.RED).strokeWidth(3f));
map.animateCamera(CameraUpdateFactory.newLatLngZoom(points[0], 18));

I can see the polygon before the tiles are starting to load but then it's gone. I would like to achieve that the polygon is drawn over the TileProvider.

I have a custom TileProvider which does three layers: cached layer, custom map layer and data layer which is partly transparent.

When map loads, I create all three layers:

cacheLayer = map.addTileOverlay(new TileOverlayOptions().tileProvider(addCache()));
mapLayer = map.addTileOverlay(new TileOverlayOptions().tileProvider(addMap()));
dataLayer = map.addTileOverlay(new TileOverlayOptions().tileProvider(addData()));

How could I achieve that the polygon is above them all? Or at least above the map layer? Should I create another overlay just for the polygon? If so, how can I draw a polygon on the TileProvider? If that is possible, I could just draw it on the dataLayer?

回答1:

Oh well, the solution is pretty simple, if anyone else will have the problem in the future:

cacheLayer = map.addTileOverlay(new TileOverlayOptions().tileProvider(addCache()).zIndex(1));
mapLayer = map.addTileOverlay(new TileOverlayOptions().tileProvider(addMap()).zIndex(2));
dataLayer = map.addTileOverlay(new TileOverlayOptions().tileProvider(addData()).zIndex(3));

And for the polygon:

map.addPolygon(new PolygonOptions().add(points).strokeColor(Color.RED).strokeWidth(3f).zIndex(4));
map.animateCamera(CameraUpdateFactory.newLatLngZoom(points[0], 18));