First launch of Activity with Google Maps is very slow

前端 未结 9 1729
礼貌的吻别
礼貌的吻别 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 13:01

    For me it was way slower than 1sec beause i was using:

    mapFragment.getMap();
    

    Then i changed to:

     mapFragment.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap googleMap) {
                map = googleMap;
            }
     });
    

    Using the getMapAsync() it won't block the ui, so your activity will load before the map. It's still slow, but at least you can show a loading message.

    0 讨论(0)
  • 2020-12-02 13:09

    Ok so I just had the same issue and think, after viewing this question, that there is no 'nice' solution.

    My current hack is to delay adding the fragment, giving the Activity a chance to render everything else before adding the map.

    Now, I am embedding the map as a childfragment, so my code looks like this:

        // inside onCreateView
        new Handler().postDelayed(new Runnable() {
    
            @Override
            public void run() {
                if (isAdded()) {
                    FragmentManager fm = getChildFragmentManager();
                    GoogleMapFragment mapFragment = GoogleMapFragment
                            .newInstance();
                    fm.beginTransaction()
                            .replace(R.id.mapContainer, mapFragment).commit();
                }
            }
        }, 1000);
    

    if adding directly to Activity it might look like this:

        // inside onCreate
        new Handler().postDelayed(new Runnable() {
    
            @Override
            public void run() {
                if (!isFinishing()) {
                    FragmentManager fm = getFragmentManager();
                    GoogleMapFragment mapFragment = GoogleMapFragment
                            .newInstance();
                    fm.beginTransaction()
                            .replace(R.id.mapContainer, mapFragment).commit();
                }
            }
        }, 1000);
    

    Nevertheless, a check inside the Runnable is needed to ensure that we are not trying to add the map to some non-existing Activity or Fragment.

    I am not a fan of hardcoded delays like this, so will return if I come up with something better. 1 second should be plenty though and could probably be even less.

    0 讨论(0)
  • 2020-12-02 13:11

    I have a "main" activity - and an activity with mapView. When that activity-with-mapView starts for a first time, it is really slow.

    clocksmith's post gave me an idea to start initialization from main activity in a separate thread. And it really solves the problem.

    Here is my code from "main" activity:

    public void onCreate(Bundle savedInstanceState) {
        ...
    
        Runnable initMap = () -> {
            BaseApplication.d("Start init mapView");
            MapView mapView = new MapView(MainActivity.this);
            mapView.onCreate(null);
            BaseApplication.d("... done");
        };
        new Thread(initMap).start();
    }
    

    mapView is never used - it's only for initialization purpose.

    And here is a stack trace - just for info:

    12-09 19:31:54.442 17172-17341/my.app D/XXX: Start init mapView
    12-09 19:31:54.525 17172-17341/my.app I/zzy: Making Creator dynamically
    12-09 19:31:55.007 17172-17341/my.app D/ChimeraCfgMgr: Reading stored module config
    12-09 19:31:55.153 17172-17341/my.app D/ChimeraCfgMgr: Loading module com.google.android.gms.maps from APK /data/user/0/com.google.android.gms/app_chimera/chimera-module-root/module-71c764a6f3cb92bdc5525a965b589e7c5ed304f3/MapsModule.apk
    
    12-09 19:31:55.154 17172-17341/my.app D/ChimeraModuleLdr: Loading module APK /data/user/0/com.google.android.gms/app_chimera/chimera-module-root/module-71c764a6f3cb92bdc5525a965b589e7c5ed304f3/MapsModule.apk
    12-09 19:31:55.262 17172-17341/my.app D/ChimeraFileApk: Primary ABI of requesting process is armeabi-v7a
    12-09 19:31:55.271 17172-17341/my.app D/ChimeraFileApk: Classloading successful. Optimized code found.
    12-09 19:31:55.316 17172-17341/my.app W/System: ClassLoader referenced unknown path: /data/user/0/com.google.android.gms/app_chimera/chimera-module-root/module-71c764a6f3cb92bdc5525a965b589e7c5ed304f3/native-libs/armeabi-v7a
    
    12-09 19:31:55.317 17172-17341/my.app W/System: ClassLoader referenced unknown path: /data/user/0/com.google.android.gms/app_chimera/chimera-module-root/module-71c764a6f3cb92bdc5525a965b589e7c5ed304f3/native-libs/armeabi
    12-09 19:31:55.618 17172-17341/my.app I/Google Maps Android API: Google Play services client version: 7571000
    12-09 19:31:55.630 17172-17341/my.app I/Google Maps Android API: Google Play services package version: 8489438
    12-09 19:31:55.969 17172-17341/my.app I/e: Token loaded from file. Expires in: 423267993 ms.
    12-09 19:31:55.969 17172-17341/my.app I/e: Scheduling next attempt in 422967 seconds.
    12-09 19:31:56.338 17172-17341/my.app D/XXX: ... done
    

    As we can see, it realy takes a lot of time...

    0 讨论(0)
提交回复
热议问题