Ok. You\'d think this would be relatively simple, but nope.
I am using Open street maps on my website, as the data is free to use, edit, and update - my project follows
The Sample Code delivered with Google Maps Android API v2 contains the class com.example.mapdemo.TileOverlayDemoActivity. I little update to this class will show OpenStreetMap tiles within the new v2 framework. Cool! No need to use osmdroid or sherlock-fragments anymore. However... the tiles are not cached :(
Anyway, all additions/updates to the TileOverlayDemoActivity class are shown below:
private static final String OPEN_STREET_MAP_URL_FORMAT =
"http://tile.openstreetmap.org/%d/%d/%d.png";
private void setUpMap() {
mMap.setMapType(GoogleMap.MAP_TYPE_NONE);
mMap.setMyLocationEnabled(true);
TileProvider tileProvider = new UrlTileProvider(256, 256) {
@Override
public synchronized URL getTileUrl(int x, int y, int zoom) {
String s = String.format(Locale.US, OPEN_STREET_MAP_URL_FORMAT, zoom, x, y);
URL url = null;
try {
url = new URL(s);
} catch (MalformedURLException e) {
throw new AssertionError(e);
}
return url;
}
};
mMap.addTileOverlay(new TileOverlayOptions().tileProvider(tileProvider));
}
Before the new Google Maps API came out, I was forced into using OSMDroid for my purposes. However, after having to dig through the source several times to figure out why it was doing what it was (often incorrectly), I was dying for a new library.
Fortunately, the new Google Maps API fits the bill. You can use the Google Maps API v2 in your application and never have to use Google's basemaps, but yes, unfortunately you must get an API key first.
I am assuming you want to use OpenStreetMap as an online map source, so the code below will be tailored towards that.
GoogleMap map = <get-map-instance>;
map.setMapType(GoogleMap.MAP_TYPE_NONE);
TileOverlayOptions options = new TileOverlayOptions();
options.tileProvider(new UrlTileProvider(256, 256) {
@Override
public URL getTileUrl(int x, int y, int z) {
try {
String f = "http://tile.openstreetmap.org/%d/%d/%d.png";
return new URL(String.format(f, z, x, y));
}
catch (MalformedURLException e) {
return null;
}
}
});
map.addTileOverlay(options);
You can find a more formalized version of this code in my Google Maps API v2 add-ons library here.
Also, if you are targeting pre-Fragment Android versions, which I believe you are based on you using ABS, instead of using MapFragment, make sure you use SupportMapFragment.