javaxian
javaxian

Reputation: 2374

Maps Android API doesn't work offline without online initialization

I'm using Maps Android API in the offline mode extensively in travel apps I build. On the first app launch, I download all the tiles I need so that they're available later in the field without the Internet. Everything works great, but I noticed that the Maps API does require Internet connection on its first use after app installation. The framework probably performs API key validation to make sure it's legit.

Since my fragments containing com.google.android.gms.maps.MapView are not displayed on the first screen, there's a risk a user downloads the map for offline use in a hotel, goes into the wild, and... kaboom! - map is not displayed.

How to initialize Android Map framework so that maps are available later when there's no connection? Is there a way to skip online key validation?

Upvotes: 1

Views: 3084

Answers (2)

javaxian
javaxian

Reputation: 2374

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).

Upvotes: 1

Gordon developer
Gordon developer

Reputation: 417

If you are aiming at caching google map's tiles for offline use then you may be violating their terms,You are first required to purchase their enterprise Maps API Premier, check this link How to cache Google map tiles for offline usage?

Upvotes: 0

Related Questions