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

时间:2014-10-08 20:09:19

标签: android android-maps-v2 android-maps

效果提升:

之前我在drawable文件夹中保存了所有图像,这可能是地图首次加载速度慢的原因,当在屏幕上绘制标记时,图像可能与屏幕尺寸不符。 现在我将图片保存在drawable-mdpidrawable-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"/>

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

5 个答案:

答案 0 :(得分:43)

我在我的应用程序中使用了非常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();

这将在后台线程(远离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();

答案 1 :(得分:5)

为了补充@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();

你必须实现这个:

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

并在您的活动布局中:

<FrameLayout
    android:id="@+id/dummy_map_view"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:visibility="gone"/>

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

答案 2 :(得分:3)

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

在此处查看我的相关信息:First launch of Activity with Google Maps is very slow

答案 3 :(得分:0)

[EDITED]

getMap()setUpMap()方法可能非常慢。他们的处理应该在AsyncTask中完成,以便onCreateView()每次都能快速返回。

更多信息:在UI线程上调用onCreateView(),因此任何缓慢的处理都会冻结UI。您的AsyncTask应在其doInBackground()方法中执行所有缓慢处理,然后使用其onPostExecute()方法更新UI。有关详细信息,请参阅AsyncTask JavaDoc

答案 4 :(得分:-4)

你可以试试这个

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mapActivity);
    getSreenDimanstions();
    fragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));

    map = fragment.getMap();    

}

此课程扩展了活动

相关问题