Question

I have a map with multiple map types that you can choose from (default Google Maps Tiles (online), OSM tiles (offline), My other custom map(offline)).

Also I have 394 markers on the map (but it's just a small map of ONE town... not important)

The problem is, it's posiible to change between google map types, because theres function for it, without clearing the map and creating and inserting markers all over again.

But if I select for example OSM map tiles, they're added like Overlay, so when I want to change them to another map, including Google Maps, I have to call mMap.clear() and then add all markers and different map tiles or change type of google map.

I know it's possible to delete specific marker by saving it in variable and then call remove() on it. Is there something similar with map overlays?

I add map tile overlays like this:

mMap.addTileOverlay(new TileOverlayOptions().tileProvider(new CustomMapTileProvider(m_context,MAP_TYPE_OSM)));
Was it helpful?

Solution

The thread opener has solved his question himself:

You need to store the return value of the gmap.addTileOverlay() method to a variable and can then call .remove() on it (just as you would with markers):

public class Map {
    private TileOverlay tileOverlay;
    ...

    void replaceOverlayTiles() {

        TileProvider tileProvider = new UrlTileProvider(256, 256) {
        @Override
        public URL getTileUrl(int x, int y, int zoom) {
            String s = String.format(Locale.GERMANY, "http://www.domain.com/xyz_tiles/%d/%d/%d.png", zoom, x, y);

            if (!checkTileExists(x, y, zoom))
                return null;

            try {
                return new URL(s);
            } catch (MalformedURLException e) {
                throw new AssertionError(e);
            }
        }

        private boolean checkTileExists(int x, int y, int zoom) {
            return !(zoom < 0 || zoom > 20);
        }
    };

    if (tileOverlay != null)
        tileOverlay.remove();

    tileOverlay = googleMap.addTileOverlay(new TileOverlayOptions().fadeIn(false).tileProvider(tileProvider));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top