Offline Mode for Android App Using the Google Maps API

Offline mode for Android app using the Google Maps API

See this solution or this one. Basically you just create your own tileprovider and access tiles locally. It is totally doable with the v2 API. API Reference

There's some false information floating around out there that the v2 Google Maps API requires an Internet connection. There was a but where the API would require a single access after app install to verify with Google Play services, but I believe this has been fixed.

See this and this.

Maps Android API doesn't work offline without online initialization

After some experimenting I found out a simple solution.

So, first, in my first activity layout (it's a host activity for all my fragments) I added the following zero-sized invisible MapView:

<com.google.android.gms.maps.MapView
android:id="@+id/dummyMapViewToInitForOfflineUse"
android:layout_width="0dp"
android:layout_height="0dp"
android:visibility="invisible"
/>

Then, in the activity code, I added the following method:

private void initGoogleMapFrameworkToMakeItUsableOfflineLater() {
dummyMapViewToInitForOfflineUse.onCreate(new Bundle());
dummyMapViewToInitForOfflineUse.getMapAsync(ignored -> {
Timber.d("GoogleMap framework initialized and ready to use offline later");
});
}

You can call it in onCreate as well as at any other reasonable moment (I use AndroidAnnotations, so I called it from my init method annotated with @AfterViews). Obvoiusly, if you don't use AndroidAnnotations or other view binding framework, you need to perform findViewById(R.id.dummyMapViewToInitForOfflineUse).

Use google maps offline maps (cache) in Google Maps Android API

You need to create your own TileProvider and access tiles locally. Check this documentation.

Here are some related threads that may help you:

  • Using the android Google Maps API v2 as a viewer of offline tiles: is it possible?
  • Offline mode for android app using google maps api
  • TileProvider using local tiles

Check this video tutorial about caching and this example from GitHub.

You can also use osmdroid which is a replacement for Android's MapView class. It also includes a modular tile provider system with support for numerous online and offline tile sources and overlay support with built-in overlays for plotting icons, tracking location, and drawing shapes. Here is the tutorial.

org.osmdroid.views.MapView mapView = (org.osmdroid.views.MapView) findViewById(R.id.map_view); //resolve the map view by id given in the layout
mapView.setTileSource(new OnlineTileSourceBase("Google Maps", ResourceProxy.string.unknown, 1, 20, 256, ".png", "http://mt3.google.com/vt/v=w2.97") {
@Override
public String getTileURLString(final MapTile aTile) {
/*
* GOOGLE MAPS URL looks like
* base url const x y zoom
* http://mt3.google.com/vt/v=w2.97&x=74327&y=50500&z=17
*/
return getBaseUrl() + "&x=" + aTile.getX() + "&y=" + aTile.getY() + "&z=" + aTile.getZoomLevel();
}
});
mapView.setUseDataConnection(false); //this actually makes the controller use only offline tiles

Hope this helps!



Related Topics



Leave a reply



Submit