First launch of Activity with Google Maps is very slow

前端 未结 9 1793
礼貌的吻别
礼貌的吻别 2020-12-02 12:37

I want to have SupportMapFragment in one of my Activity. I add this fragment directly to layout xml and this layout set as content view. But when Activity is launched for th

9条回答
  •  情书的邮戳
    2020-12-02 12:52

    The reason why the first load takes so long is because the Play Services APIs have to load as seen in log lines:

    I/Google Maps Android API﹕ Google Play services client version: 6587000
    I/Google Maps Android API﹕ Google Play services package version: 6768430
    

    Unfortunately, the "package" one takes about a second to load and using the MapsInitializer only will get you the "client." So here is a a not so pretty work around: Initialize a dummy map in your main launcher activity.

    mDummyMapInitializer.getMapAsync(new OnMapReadyCallback() {
      @Override
      public void onMapReady(GoogleMap googleMap) {
        Log.d(TAG, "onMapReady");
      }
    });
    

    Now when you load your actual map later on, it shouldn't have to initialize the Play services APIs. This shouldn't cause any delays in your main activity either because the async method executes off the main thread.

    Since you have to do the initialization somewhere no matter what, I think it makes sense to do it right when the app starts, so that when you load an activity that actually needs a map, you do not have to wait at all.

    Note: mDummyMapInitializer must be a MapFragment or SupportMapFragmentand must be added to the activity, or else the Play Services APIs won't be loaded. The getMapAsync method itself must also be called from the main thread.

提交回复
热议问题