Open Street Maps with Android Google Maps Api v2

痴心易碎 提交于 2019-11-28 21:27:31

You need to extend the UrlTileProvider class so you can define the URL for OSM tiled maps and add a tile overlay like that :

MyUrlTileProvider mTileProvider = new MyUrlTileProvider(256, 256, mUrl);
mMap.addTileOverlay(new TileOverlayOptions().tileProvider(mTileProvider));

With the url for OSM defined like that :

String mUrl = "http://a.tile.openstreetmap.org/{z}/{x}/{y}.png";

The MyUrlTileProvider class :

public class MyUrlTileProvider extends UrlTileProvider {

private String baseUrl;

public MyUrlTileProvider(int width, int height, String url) {
    super(width, height);
    this.baseUrl = url;
}

@Override
public URL getTileUrl(int x, int y, int zoom) {
    try {
        return new URL(baseUrl.replace("{z}", ""+zoom).replace("{x}",""+x).replace("{y}",""+y));
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    return null;
}
}

I am now trying to get those tiled maps from OSM with an Offline Mode so if anyone get a quick solution, please let me know!

When using this approach, please pay attention to the OSM Tile Usage Policy: http://wiki.openstreetmap.org/wiki/Tile_usage_policy

Specifically "Heavy use (e.g. distributing an app that uses tiles from openstreetmap.org) is forbidden without prior permission"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!