我想让谷歌地图v2在我的应用程序中工作.我已经看到几个示例显示如何在活动中打开SupportMapFragment.想法是你的活动将调用setContentView(R.layout.map_layout);其中map_layout.xml链接到带有行的片段:
android:name="com.google.android.gms.maps.SupportMapFragment" xmlns:map="http://schemas.android.com/apk/res-auto"
“name =”行有效地表示“此布局由”SupportMapFragment“类型的片段控制.
我的复杂之处在于我试图让地图显示在带有标签的活动中(使用actionbarsherlock实现).这意味着对应于选项卡选择的任何片段都必须实现TabListener.但是SupportMapFragment没有.所以现在我想要创建一个像这样的新片段:
public class MyMapFragmentWithTabListener extends SupportMapFragment implements TabListener {
但是现在我对如何编写MapFragmentWithTabListener的内容感到困惑,特别是在onCreateView上…我应该膨胀一些布局吗?当然我不能从示例中完全膨胀相同的map_layout.xml,因为它已经声明它由SupportMapFragment控制,而在此实现中它应该由MyMapFragmentWithTabListener控制 – 我是否需要稍微不同的xml文件来膨胀(如果它应该是什么样子?) – 或者我应该以编程方式创建我的视图?
解决方法
我现在已经在很多应用程序中完成了这个.您只需创建自己的MapFragment,而不是扩展SupportMapFragment.您可以拥有自己的布局,其中包含MapView视图.关键是将Fragment的生命周期事件路由到MapView,然后让你的叔叔陷入困境.
下面是一些示例代码:
MapFragment
package com.example.testapplication; import java.util.concurrent.TimeUnit; import com.google.android.gms.common.GooglePlayServicesNotAvailableException; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.MapView; import com.google.android.gms.maps.MapsInitializer; public class TestMapFragment extends Fragment { private MapView mMapView; @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) { View view = inflater.inflate(R.layout.home_fragment,container,false); mMapView = (MapView) view.findViewById(R.id.mapview); // inflat and return the layout mMapView.onCreate(savedInstanceState); mMapView.onResume();// needed to get the map to display immediately try { MapsInitializer.initialize(getActivity()); } catch (GooglePlayServicesNotAvailableException e) { e.printStackTrace(); } GoogleMap googleMap = mMapView.getMap(); googleMap.getUiSettings().setMyLocationButtonEnabled(true); return view; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); } /* * Using a mapview in a fragment requires you to 'route' * the lifecycle events of the fragment to the mapview */ @Override public void onResume() { super.onResume(); if (null != mMapView) mMapView.onResume(); } @Override public void onPause() { super.onPause(); if (null != mMapView) mMapView.onPause(); } @Override public void onDestroy() { super.onDestroy(); if (null != mMapView) mMapView.onDestroy(); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if (null != mMapView) mMapView.onSaveInstanceState(outState); } @Override public void onLowMemory() { super.onLowMemory(); if (null != mMapView) mMapView.onLowMemory(); } }
和布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:map="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" > <com.google.android.gms.maps.MapView android:id="@+id/mapview" android:layout_width="match_parent" android:layout_height="match_parent" map:uiZoomControls="false" /> </RelativeLayout>