是什么让我的地图片段加载缓慢?

Hai*_*ang 31 android android-maps android-maps-v2

绩效提升:

以前我将所有图像保存在drawable文件夹中,这可能是地图首次加载缓慢的原因,当在屏幕上绘制标记时,图像可能不适合屏幕大小.现在我保存了图像drawable-mdpi,drawable-hdpi等等,应用程序比以前更顺畅.希望能帮助到你

原始问题:

我在片段中创建了一个地图,源代码可以在下面找到.

第一次加载时,地图片段缓慢.如果我再去任何其他片段并再次点击地图片段,它就会加载得很快而且没有slug.

谁能告诉我这里发生了什么?谢谢!

fragment_map.xml,id是 map

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:map="http://schemas.android.com/apk/res-auto"
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.SupportMapFragment"/>
Run Code Online (Sandbox Code Playgroud)

MyMapFragment.java(包含onCreateViewsetUpMapIfNeeded)

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    try {
        rootView = inflater.inflate(R.layout.fragment_map, container, false);
    } catch (InflateException e) {
    /* map is already there, just return view as it is */
        Log.e(TAG, "inflateException");
    }

     setUpMapIfNeeded();

    return rootView;
}


public void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the fragment_map.
        if (myMap == null) {
            // Try to obtain the fragment_map from the SupportMapFragment.
            myMap = ((SupportMapFragment) MainActivity.fragmentManager.findFragmentById(R.id.map)).getMap();
            // Check if we were successful in obtaining the fragment_map.
            if (myMap != null) {
                setUpMap();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

Xya*_*ren 45

我在我的应用程序中使用了一种非常hackish但有效的方法,但它运行良好.应用程序启动后,我的mapFragment不会立即显示!否则这没有意义.

把它放在你的启动器活动的onCreate中:

    // Fixing Later Map loading Delay
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                MapView mv = new MapView(getApplicationContext());
                mv.onCreate(null);
                mv.onPause();
                mv.onDestroy();
            }catch (Exception ignored){

            }
        }
    }).start();
Run Code Online (Sandbox Code Playgroud)

这将在后台线程(远离ui)中创建mapview,并使用它初始化所有Google Play服务和地图数据.

加载的数据额外约为5MB.

如果有人有一些改进的想法请随时评论!


Java 8版本:

// Fixing Later Map loading Delay
new Thread(() -> {
    try {
        MapView mv = new MapView(getApplicationContext());
        mv.onCreate(null);
        mv.onPause();
        mv.onDestroy();
    }catch (Exception ignored){}
}).start();
Run Code Online (Sandbox Code Playgroud)

  • 实际上,我很乐意给你赏金.为此开始赏金是否可以,还是"违反规则"?严肃地问.. (2认同)
  • 尝试使用`runOnUiThread()`而不是`new Thread()运行UI线程上的Runnable.start()` (2认同)

Con*_*nti 6

只是为了添加@Xyaren的答案,我需要它为SupportFragment工作,这似乎需要一些额外的步骤.让您的活动实现OnMapReadyCallback.

在你的onCreate:

 new Thread(() -> {
        try {
            SupportMapFragment mf = SupportMapFragment.newInstance();
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.dummy_map_view, mf)
                    .commit();
            runOnUiThread(() -> mf.getMapAsync(SplashActivity.this));
        }catch (Exception ignored){
            Timber.w("Dummy map load exception: %s", ignored);
        }
    }).start();
Run Code Online (Sandbox Code Playgroud)

你必须实现这个:

@Override
 public void onMapReady(GoogleMap googleMap) {
    // Do nothing because we only want to pre-load map.
    Timber.d("Map loaded: %s", googleMap);
}
Run Code Online (Sandbox Code Playgroud)

并在您的活动布局中:

<FrameLayout
    android:id="@+id/dummy_map_view"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:visibility="gone"/>
Run Code Online (Sandbox Code Playgroud)

它需要完成所有步骤,包括Google Play服务的交易以下载地图数据.


Woo*_*kie 5

我也经常遇到这个问题......原来最大的罪魁祸首是将调试会话附加到我的应用程序进程中。当我断开调试器的连接或拔掉 USB 电缆时,我的应用程序中的地图内容运行得更快、更流畅。

在此处查看我的相关帖子:首次启动 Activity with Google Maps 非常慢