17

Is it possible to use an open street map provider with the new Google Maps V2 Api on Android? If so can you provide an example, or documentation? I have looked quickly at the docs and found UrlTileProvider() , so it looks likely this is possible.

Bonus: Is simply using the MapFragment class with OSM tiles still bound by the Google Maps TOS?

Janusz
  • 187,060
  • 113
  • 301
  • 369
Patrick Jackson
  • 18,766
  • 22
  • 81
  • 141

2 Answers2

25

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!

Emmanuel
  • 266
  • 3
  • 2
  • Could you perhaps expand on this with a very basic implementation? – Thomas Clowes Dec 15 '12 at 23:16
  • I got the sample up and running with OSM. Works great. This with an offline mode would be killer. Just need to find out about any licensing issues.... – Patrick Jackson Feb 02 '13 at 00:47
  • @Patrick Presumably you could write an offline version by implementing TileProvider and providing your own cache to SD? It says [here](http://stackoverflow.com/a/15198767/2209533) that you need to be online when you start the app though for v2. A shame: I'm not sure whether to try that or [osmdroid](http://stackoverflow.com/a/13897459/2209533) – Rob Mar 25 '13 at 23:25
  • 1
    We use osmdroid API since our project start and it works well with online and offline data. – L. G. Jul 10 '13 at 07:42
2

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"

Paamand
  • 626
  • 1
  • 6
  • 17