首次启动 Activity with Google Maps 非常慢 [英] First launch of Activity with Google Maps is very slow

查看:20
本文介绍了首次启动 Activity with Google Maps 非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的一个活动中使用 SupportMapFragment.我将此片段直接添加到布局 xml 并将此布局设置为内容视图.但是当Activity第一次启动时,时间太长(超过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,当用户单击 Item 时,它会使用 MapFragment 启动 DetailActivity.单击项目后,在 DetailActivity 出现之前会有明显的延迟.只有我调用 setContentView 的方法 onCreate 运行超过 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.

谢谢

推荐答案

第一次加载需要这么长时间的原因是因为 Play Services 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 才能为您提供客户端".所以这里有一个不太好的解决方法:在您的主启动器活动中初始化一个虚拟地图.

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

现在,当您稍后加载实际地图时,它不应该初始化 Play 服务 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 必须是 MapFragmentSupportMapFragment 并且必须添加到活动中,否则 Play Services 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.

这篇关于首次启动 Activity with Google Maps 非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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