在片段中自定义谷歌地图

时间:2014-06-08 21:07:06

标签: android google-maps android-fragments google-maps-android-api-2

情况是我使用Fragments在主要活动中显示2个视图,这意味着我有两个标签,在第二个标签中我使用下面的代码显示谷歌地图

  public View onCreateView(LayoutInflater inflater, ViewGroup container, 
                               Bundle savedInstanceState){

        RelativeLayout dashboardMapview = (RelativeLayout) inflater.inflate(R.layout.dashboard_mapview, container, false);


        return dashboardMapview;
      }
    }

这是第二个Tab,我想自定义地图,我在上面的函数中使用了这个代码。

imap = SupportMapFragment)getSupportFragmentManager()。findFragmentById(R.id.map))。getMap();

    imap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

imap是在onCreate上声明的Google Map变量。

问题是getSupportFragmentManager显示错误。 此外,我对没有选项卡但只有地图的活动使用了相同的代码,并且工作正常。

请注意,如果我不使用上面的代码进行自定义,我可以在运行时成功显示Google地图。唯一的问题是定制。

我的dashboardMapview.xml如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />

如果没有标签但只有一个活动,xml也可以正常工作。

我正在使用Google Maps API V2

如果它有任何意义,我认为这是因为我已经膨胀并返回了一个RelativeLayout,但片段就在那里。

有关如何自定义googlemap的任何想法,这是在点击活动中的第二个标签页时使用的吗?

1 个答案:

答案 0 :(得分:0)

发现错误,更多是一个愚蠢的错误。刚刚没有在mainactivity中扩展FragmentActivity,并且没有它就不能调用getSupportFragmentManager()。