首先推出与谷歌地图的活动是很慢 [英] First launch of Activity with Google Maps is very slow

查看:248
本文介绍了首先推出与谷歌地图的活动是很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有SupportMapFragment在我的活动之一。我直接添加此片段布局xml和该布局设置为内容视图。但是,当活动启动的第一次,时间过长(超过1秒)。接下来推出的确定,需要几毫秒。

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 the first time, it takes too long (over 1 second). Next launches are ok and take few milliseconds.

我想:

  • 在删除任何初始化
  • 使用MapFragment代替SupportMapFragment
  • 在编程添加MapFragment

但没有任何帮助。地图显示没有任何问题或可疑的日志。

but nothing helped. Map is shown without any problem or suspicious log.

你有什么建议,是什么原因和如何改进呢?

Do you have any suggestion, what it causes and how to improve it?

编辑: 我有一个ListView,当用户点击项目,它启动DetailActivity与MapFragment。点击项目后有明显的延迟之前DetailActivity显示出来。只有方法的onCreate,在那里我打电话的setContentView,运行在1秒。虽然活动是在onCreate方法,有从该活性没有可见的内容。点击和展示内容之间的延时,是不是很人性化。

edit: I have a ListView and when user clicks on Item, it launches DetailActivity with MapFragment. After click on item there is a noticeable delay before DetailActivity shows up. Only method onCreate, where I call setContentView, runs over 1 second. And while activity is in onCreate method, there is no visible content from this activity. This delay between click and showing content is not very user friendly.

感谢您

推荐答案

为什么第一次加载需要这么久的原因是因为播放服务API必须加载在日志行看到:

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

不幸的是,在包一大约需要一秒钟来加载和使用MapsInitializer只会让你的客户。因此,这里是AA不是那么pretty的变通:初始化一个虚拟的地图将在主发射活动​​

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

现在,当你以后加载您的实际地图,它不应该有初始化播放服务API。这应该不会造成任何延误您的主要活动或者是因为异步方法执行关闭主线程。

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.

注: mDummyMapInitializer 必须有一个 MapFragment SupportMapFragment 和必须被添加到活动,否则播放服务API将不会被加载。该 getMapAsync 方法本身也必须被称为从主线程。

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.

这篇关于首先推出与谷歌地图的活动是很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆