我正在尝试让谷歌地图 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文件来膨胀(如果那么,它应该是什么样子?) - 或者我应该以编程方式创建我的视图?
我现在已经在很多应用程序中这样做了。您无需扩展 SupportMapFragment,只需创建自己的 MapFragment。您可以拥有自己的布局,其中包含 MapView 视图。关键是把Fragment的生命周期事件路由到MapView上,bos你大爷的。
这是一些示例代码:
地图片段
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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)