将地图(google 地图 v2)放入片段中的最佳方式
Best way to put a map (google maps v2) inside a fragment
我有一个片段 (ApartmentFragment),它是一个显示公寓详细信息的页面。我想添加一张小地图以显示公寓的位置。
为清楚起见,我使用 github 上的 MaterialNavigationDrawer,我的应用由单个 Activity(导航抽屉)和许多片段组成。
我读过的每个 Whosebug post 都没有帮助我。他们中的许多人有点困惑。我不只想要 ApartmentFragment 内的地图,我想显示其他东西加上地图。
哪种方法最好?
可以将 mapfragment 放入片段中吗?或者我需要在 activity?
中转换 ApartmentFragment
你读到的大部分内容都是正确的。您只需使用 MapView
而不是 MapFragment
https://developer.android.com/reference/com/google/android/gms/maps/MapView.html
然后在您的 ApartmenFragment 上,您必须对 mapview 进行所有回调
- onCreate(捆绑包)
- onResume()
- onPause()
- onDestroy()
- onSaveInstanceState()
- onLowMemory()
像这样:
private MapView mapView;
@Nullable @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// inflate your view `root` and then call ...
mapView = (MapView) root.indViewById(R.id.map_view);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
return root;
}
@Override public void onResume() {
super.onResume();
mapView.onResume();
}
@Override public void onPause() {
super.onPause();
mapView.onPause();
}
@Override public void onDestroyView() {
super.onDestroyView();
mapView.onDestroy();
}
@Override public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
@Override public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override public void onMapReady (GoogleMap googleMap) {
// from https://developer.android.com/reference/com/google/android/gms/maps/OnMapReadyCallback.html
// and here you can do your map stuff with `googleMap`
}
我有一个片段 (ApartmentFragment),它是一个显示公寓详细信息的页面。我想添加一张小地图以显示公寓的位置。 为清楚起见,我使用 github 上的 MaterialNavigationDrawer,我的应用由单个 Activity(导航抽屉)和许多片段组成。
我读过的每个 Whosebug post 都没有帮助我。他们中的许多人有点困惑。我不只想要 ApartmentFragment 内的地图,我想显示其他东西加上地图。 哪种方法最好? 可以将 mapfragment 放入片段中吗?或者我需要在 activity?
中转换 ApartmentFragment你读到的大部分内容都是正确的。您只需使用 MapView
而不是 MapFragment
https://developer.android.com/reference/com/google/android/gms/maps/MapView.html
然后在您的 ApartmenFragment 上,您必须对 mapview 进行所有回调
- onCreate(捆绑包)
- onResume()
- onPause()
- onDestroy()
- onSaveInstanceState()
- onLowMemory()
像这样:
private MapView mapView;
@Nullable @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// inflate your view `root` and then call ...
mapView = (MapView) root.indViewById(R.id.map_view);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
return root;
}
@Override public void onResume() {
super.onResume();
mapView.onResume();
}
@Override public void onPause() {
super.onPause();
mapView.onPause();
}
@Override public void onDestroyView() {
super.onDestroyView();
mapView.onDestroy();
}
@Override public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
@Override public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override public void onMapReady (GoogleMap googleMap) {
// from https://developer.android.com/reference/com/google/android/gms/maps/OnMapReadyCallback.html
// and here you can do your map stuff with `googleMap`
}