图形页面中的一个片段(蜂窝)蜂窝、片段、图形、页面

2023-09-11 11:31:44 作者:loser(遗失者)

现在,最后的SDK是用谷歌的API - 什么是创建一个MapView的一个片段的最佳方式是什么?图形页面需要MapActivity工作的权利。

now that the final SDK is out with google apis - what is the best way to create a Fragment with a MapView? MapView needs a MapActivity to work right.

具有活动管理的片段从MapActivity继承(不好的解决方案,因为它违背了想法,片段是自包含的),并使用常用的基于XML的布局不起作用。我得到了MapActivity.setupMapView()一个NullPointerException异常:

Having the Activity managing the Fragments inherit from MapActivity (bad solution because it goes against the idea that Fragments are self contained) and use a regular xml based layout does not work. I get a NullPointerException in MapActivity.setupMapView():


E/AndroidRuntime(  597): Caused by: java.lang.NullPointerException
E/AndroidRuntime(  597):    at com.google.android.maps.MapActivity.setupMapView(MapActivity.java:400)
E/AndroidRuntime(  597):    at com.google.android.maps.MapView.(MapView.java:289)
E/AndroidRuntime(  597):    at com.google.android.maps.MapView.(MapView.java:264)
E/AndroidRuntime(  597):    at com.google.android.maps.MapView.(MapView.java:247)

我的第二个想法是,以编程方式创建的图形窗口和传递相关的活动(通过getActivity())作为背景来的MapView的构造。不工作:

My second idea was to create the MapView programmatically and pass the associated activity (via getActivity()) as Context to the MapView constructor. Does not work:


E/AndroidRuntime(  834): Caused by: java.lang.IllegalArgumentException: MapViews can only be created inside instances of MapActivity.
E/AndroidRuntime(  834):    at com.google.android.maps.MapView.(MapView.java:291)
E/AndroidRuntime(  834):    at com.google.android.maps.MapView.(MapView.java:235)
E/AndroidRuntime(  834):    at de.foo.FinderMapFragment.onCreateView(FinderMapFragment.java:225)
E/AndroidRuntime(  834):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:708)
E/AndroidRuntime(  834):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:900)
E/AndroidRuntime(  834):    at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:978)
E/AndroidRuntime(  834):    at android.app.Activity.onCreateView(Activity.java:4090)
E/AndroidRuntime(  834):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:664)

真的应该有类似MapFragment这需要后台照顾线程图形页面需要我猜...那么,什么是做到这一点当前的最佳做法是什么?

Really there should be something like MapFragment that takes care of the background threads MapView needs I guess... So what is the current best practice to do this?

感谢和问候来自德国, 瓦伦丁

Thanks and regards from Germany, Valentin

推荐答案

随着2012年3月12号谷歌发布的谷歌地图Android的API V2 。现在,你可以忘掉这些问题。 https://developers.google.com/maps/documentation/android/

As of 03.12.2012 Google released Google Maps Android API v2. Now you can forget about these problems. https://developers.google.com/maps/documentation/android/

例如使用新的API - https://developers.google.com/maps/documentation/android/start#add_a_map

Example using new API - https://developers.google.com/maps/documentation/android/start#add_a_map

这个API将工作至少Android的API 8,所以用它。)

This API will work for at least Android API 8, so use it ;).

所以,现在你可以简单地使用com.google.android.gms.maps.MapFragment片段类。它会显示在你的活动地图。

So now you can simply use "com.google.android.gms.maps.MapFragment" fragment class. It will display the map in your Activity. Layout example from the link above:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.MapFragment"/>