【发布时间】:2017-08-31 03:30:39
【问题描述】:
很长时间以来,我使用此代码在 Fragment 中“重用”GoogleMap - 当切换到另一个 Fragment 并再次打开 MapFragment 时,地图及其所有内容与以前一样(不再放大,无需重新设置标记,它“只是在后台”并再次被带到前面):
public class MapFragmentGoogle extends Fragment implements SensorEventListener, OnMapLoadedCallback {
private static View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (rootView != null) {
ViewGroup parent = (ViewGroup) rootView.getParent();
if (parent != null)
parent.removeView(rootView);
}
try {
rootView = inflater.inflate(R.layout.fragment_map_google, container, false);
} catch (InflateException e) {
//
}
return rootView;
所以现在我从 eclipse 切换到 AndroidStudio(我知道我迟到了),它正在实现最新的 PlayServices,我无法使用
map = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.gmap)).getMap();
“重用代码”不再起作用了。我对这些事情感到厌烦了。
我怎样才能有一个谷歌地图,它保持原样,没有新的初始化,没有从空间到地方的缩放,在切换到另一个片段然后回到地图片段时不重绘折线和标记等?
【问题讨论】:
标签: android google-maps android-fragments google-maps-android-api-2 supportmapfragment