1

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?

javaxian
  • 1,815
  • 1
  • 21
  • 26
  • Downloading and caching tiles doesn't require a `MapView` instance to be initialized, so simply starting a background service once the app is installed and fetching the necessary tiles seems like your best bet. – Nathan Reline Sep 05 '17 at 13:31
  • You're right, but displaying them later in a `MapView' without Internet connection does require it initialized, and that's what my issue is about. – javaxian Sep 05 '17 at 19:45

2 Answers2

1

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

javaxian
  • 1,815
  • 1
  • 21
  • 26
  • 1
    you dont need a mapview element in the layout file, you can create a mapview instance programatically then call .onCreate(bundle()) then getmapasync, i tested on api 28 emulator works fine – IulianT Feb 07 '20 at 12:33
0

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?

Gordon developer
  • 387
  • 3
  • 13