我在查看寻呼机中显示谷歌地图片段时遇到问题。我有一个活动,viewpager在其中,在viewpager的一个片段中有一个谷歌地图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));
}
}
所以,问题如下:当我切换到这个片段时,map似乎初始化,但是我看到静态地图快照不是可移动的地图,而是以0,0坐标为中心。
BUT!如果我锁定设备屏幕然后解锁它,地图会正常初始化!
有人可以帮助我吗?
答案 0 :(得分:1)
我有同样的问题,我使用getFragmentManager()而不是getChildFragmentManager()解决了,我使用了相同的实现:
FragmentManager fm = getFragmentManager();
fragment = (SupportMapFragment) fm.findFragmentById(R.id.map_container);
if (fragment == null) {
fragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.map_container, fragment).commit();
fm.executePendingTransactions();
}
我改变了,我的问题解决了......我希望这对你有帮助。
答案 1 :(得分:0)
您是否尝试初始化地图onActivityCreated?
if (mapFragment == null) {
mapFragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.about_map, mapFragment).commit();
fm.executePendingTransactions();
}
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!");
}
}