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

2023-09-04 06:50:27 作者:兔萌萌i

林创造了一个片段的地图,源$ C ​​$ C可以在下面找到。

Im creating a map in a fragment, the source code can be found below.

该地图可以显示出来,遗憾的是当第一次地图碎片载荷,看起来呆滞,如果我去其他任何片段,然后点击地图上的片段,它加载速度快,不塞了。

The map can show up, unfortunately when the first time the map fragment loads, it looks sluggish, if I go any other fragment and click the map fragment, it loads fast and no slug anymore.

谁能告诉我是怎么回事呢?谢谢!

Can anyone tell me what is going on here? Thanks!

fragment_map.xml,id为地图

fragment_map.xml, id is 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"/>

MyMapFragment.java(包含 onCreateView setUpMapIfNeeded

@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();
            }
        }
    }

编辑: previously我存储的所有图像绘制文件夹,这可能是为什么在地图首次加载缓慢,当在屏幕上绘制了标记,图像可能不适合的原因屏幕尺寸。 在绘制-MDPI 绘制 - 华电国际等,在现在我存储的图像应用程序的工作比以前更顺畅。

Previously I stored all images in drawable folder, this might be the reason why the map first loads slow, when draw the markers on screen, the image may not fit the screen size. Now I stored images in drawable-mdpi, drawable-hdpi and so on, the app works smoother than before.

推荐答案

我用我的应用程序非常hakish而有效的方法,但它的作品好。 在应用程序启动后,我mapFragment没有显示正确的!否则,这是没有意义的。

I'm using a very hakish but effective way in my application, but it works good. My mapFragment is not displayed right after the app launches! Otherwise this would not make sense.

在将这个你的发射活动的的onCreate

Put this in your launcher activity's 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();

这将创建一个在后台线程一个图形页面(远离UI),并用它,初始化所有的谷歌播放服务和地图数据。

This will create a mapview in an background thread (far away from the ui) and with it, initializes all the google play services and map data.

加载的数据大约是5MB多。

The loaded data is about 5MB extra.

如果有人有一些想法,改进随意评论请!

If someone has some ideas for improvements feel free to comment please !