Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open Street Maps with Android Google Maps Api v2

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?

like image 237
Patrick Avatar asked Dec 04 '12 02:12

Patrick


People also ask

How do I get an open street map API?

The Openstreetmap API endpoint is located at http://api.openstreetmap.org/. You can find the Openstreetmap API portal / hompage here. If you need Openstreetmap API support, you can visit developer support here, or reach out to their Twitter account at @OpenStreetMap.

Is Open Street maps API free?

The OpenStreetMap API is free.

How do I enable Google Maps API v2?

Click on the “APIs & auth” menu on the left, and from the submenu select APIs. From the list of APIs that appear, scroll down and ensure that Google Maps Android API v2 is set to “On”.

Does OpenStreetMap have an API?

OpenStreetMap has an editing API for fetching and saving raw geodata from/to the OpenStreetMap database — this is the entry page for the documentation. If you just want to embed a map into a webpage, you don't want this API. Use a web map library instead.


1 Answers

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!

like image 90
Emmanuel Avatar answered Oct 19 '22 11:10

Emmanuel