如何在每个 Android 应用程序/进程中使用多个 MapActivities/MapView
我编写了一个 MapActivity 类,它能够显示一组地点以及单个地点。启动时,应用程序会创建此 MapActivity 的实例并显示多个位置。如果用户单击某个地点,则会启动一个新的 Activity
,显示所选地点的详细信息。此活动有一个菜单项,允许用户查看地图上的地点 - 这会导致创建 MapActivity 的新实例,但现在仅显示这个地点。
现在的问题是,如果用户导航回第一个 MapActivity
(显示多个位置的那个),则图块将不再加载+有时会遇到 OutOfMemoryErrors。
根据 Android JavaDocs,每个进程只能有一个 MapActivity
。但是,我不想将我的 MapActivity
定义为 singleInstance/singleTask,因为用户应该始终能够导航回显示多个位置的第一个 MapActivity。
我发现 Android 版 Google Places 应用(随 Google Map 4.4 一起提供)使用多个 MapActivity
实例。这怎么可能?我怎样才能在我的应用程序中实现它?
I have written one MapActivity
class that is able to display a set of places as well as single places. On startup, the application creates an instance of this MapActivity
and displays multiple places. If the user clicks on a certain place, then a new Activity
is launched that shows the details of the selected place. This activity has a menu item that allows the user to view the place on a map - this causes that a new instance of the MapActivity
is created, except that now only this single place is displayed.
The problem now is that if the user navigates back to the first MapActivity
(the one that shows multiple places) the tiles won't be loaded anymore + sometimes OutOfMemoryErrors are encountered.
According to the Android JavaDocs, it is only possible to have one MapActivity
per process. However, I don't want to define my MapActivity
as a singleInstance/singleTask, since the user should always be able to navigate back to the first MapActivity that shows multiple places.
I have seen that the Google Places app (which has come with Google Map 4.4) for Android uses multiple MapActivity
instances. How is this possible? And how can I achieve it in my application?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它不是每个类一个地图视图,而是每个进程。
众所周知,在一个进程中使用多个地图视图时可能会遇到一些问题。如果您没有配置任何特定的内容,通常会出现这种情况(您的应用程序在一个进程中运行)。
不过,您可以在清单中使用 android:process 属性来分配给您的活动:
这样您就可以在单独的进程中运行活动,如果您不跨活动使用任何共享静态变量,则效果很好。
另请参阅 android bug tracker 中有关该 bug 的讨论:
http:// code.google.com/p/android/issues/detail?id=3756
It's not one map view per class, it's per process.
It's known that you might experience some issues when using multiple mapviews in one process. Usually this is the case (your app running in one process) if you don't configure anything specific.
You could though use the android:process attribute in your manifest to assign to your acitivites:
This way you have the activities running in separate processes, which works well if you don't use any shared static variables across activities.
Also see the discussion on the bug in the android bug tracker:
http://code.google.com/p/android/issues/detail?id=3756
每个进程可以有多个 MapActivity,只要一次只有一个在运行。每个 MapActivity 只能分配一个 MapView。您可以通过在同一个MapActivity中重复使用同一个MapView来显示不同的地图。您可以通过将 MapView 声明为静态类变量来重用它,在显示完它后将其从当前添加到的视图中删除,为其提供新的 GPS 坐标并将其添加到下一个视图。当您完成该 MapActivity 后,您就可以打开一个新的 MapActivity。这有效,我在我的应用程序中这样做。
You can have more than one MapActivity per process as long as only one is running at a time. Each MapActivity can have only one MapView assigned to it. You can show different maps by reusing the same MapView in the same MapActivity. You can reuse a MapView by declaring it as a static class variable, removing it from the view that it was currently added to when you are done displaying it, giving it new GPS coordinates and adding it to the next view. When you are finished with that MapActivity finish it and then you can open a new MapActivity. This works, I do in my app.
另一个简单的解决方案:只需重写所有 MapActivities 的 onDestroy 方法以防止关闭资源,例如:
我们应该在此处调用 onStop 以防止抛出 RuntimeException。是的,这是一个 hack,但它确实有效。
Another simple solution: just override onDestroy method of all of your MapActivities to prevent closing resources, e.g.:
We should invoke onStop here to prevent RuntimeException thrown. Yes, that's a hack but it works.
更新:
Google Maps Android API v2 支持 Fragment。这意味着您可以像显示任何其他片段一样轻松显示多个地图。
请参阅文档并详细了解新的
MapFragment
类。Update:
The Google Maps Android API v2 has support for Fragments. This means that you can easily display multiple maps like you would any other fragment.
See the documentation and read more about the new
MapFragment
class.