Android Java google maps TileOverLay "OSM "

Viewed 45
@Override
public void onMapReady(GoogleMap map) {
   map.setMapType(GoogleMap.MAP_TYPE_NONE); 
   TileProvider tileProvider = new UrlTileProvider(256, 256) {
        @Override
        public synchronized URL getTileUrl(int x, int y, int zoom) {
        String s = String.format("https://tile.openstreetmap.org/%d/%d/%d.jpg", zoom, x, y);
           URL url = null;
            try {
                url = new URL(s);
            } catch (MalformedURLException e) {
                throw new AssertionError(e);
            }
            return url;
        }
    };
   map.addTileOverlay(new TileOverlayOptions().tileProvider(tileProvider));
}

Why does the Open StreetMap Tile do not show up in my Maps? Is Google preventing it? https://tile.openstreetmap.org/%d/%d/%d.png

1 Answers

You have to use user-agent to use osm map tile servers.

public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36";

@Override
    public void onMapReady(GoogleMap map) {
       map.setMapType(GoogleMap.MAP_TYPE_NONE); 
       TileProvider tileProvider = new UrlTileProvider(256, 256) {
            @Override
            public synchronized URL getTileUrl(int x, int y, int zoom) {
            String s = String.format("https://tile.openstreetmap.org/%d/%d/%d.jpg", zoom, x, y);

            System.setProperty("http.agent", USER_AGENT);

               URL url = null;
                try {
                    url = new URL(s);
                } catch (MalformedURLException e) {
                    throw new AssertionError(e);
                }
                return url;
            }
        };
       map.addTileOverlay(new TileOverlayOptions().tileProvider(tileProvider));
    }
Related