我在视图寻呼机中显示谷歌地图片段时遇到问题。我有一个活动,其中有 viewpager,在 viewpager 的一个片段中有一个 google map 2.0 片段。
片段布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical" >
<TextView
android:id="@+id/no_profile_dialog_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="@string/about_screen_info" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:scaleType="fitCenter"
android:src="@drawable/banner_bf" />
<fragment
android:id="@+id/about_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
android:tag="AboutMapFragment"
map:mapType="normal" />
</LinearLayout>
片段类:
public class AboutFragment extends Fragment {
public static final String TAG = AboutFragment.class.getSimpleName();
private SupportMapFragment mapFragment;
private GoogleMap map;
public static AboutFragment newInstance() {
AboutFragment fragment = new AboutFragment();
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.fragment_about, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FragmentManager fm = getChildFragmentManager();
mapFragment = (SupportMapFragment) fm.findFragmentById(R.id.about_map);
if (mapFragment == null) {
mapFragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.about_map, mapFragment).commit();
fm.executePendingTransactions();
}
}
@Override
public void onResume() {
super.onResume();
if (map == null) {
map = mapFragment.getMap();
if(map != null){
Log.d(TAG, "map in AboutFragment initialized!");
drawPositionOnMap();
}else{
Log.d(TAG, "map in AboutFragment is null!");
}
}
}
private void drawPositionOnMap() {
LatLng latLng = new LatLng(20, 50);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 20));
map.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
map.addMarker(new MarkerOptions().position(latLng));
map.setMyLocationEnabled(true);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
((HomeActivity)getActivity()).getCurrentSlidingMenu().addIgnoredView(view.findViewById(R.id.about_map));
}
}
所以,问题如下:当我切换到这个片段时,地图似乎正在初始化,但我看到的不是可移动地图,而是以 0,0 坐标为中心的静态地图快照。
但!如果我锁定设备屏幕然后解锁它,地图会正常初始化!
有人可以帮我吗?