我还没有带导航抽屉的活动。 地图显示在包含一些其他控件的片段内。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="@string/incident" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="@string/notify_work" />
</RelativeLayout>
在第一次加载时一切正常,但是当我在模拟器中更改方向时,只显示带有工具栏和按钮的空白屏幕。 我正在使用带有扩展库的map v2。
遵循片段代码:
public class MapFragment extends Fragment {
private GoogleMap mCurrentMap = null;
private static MapFragment mapFragment;
private SupportMapFragment supportMapFragment;
public static MapFragment newInstance() {
if(mapFragment == null) {
mapFragment = new MapFragment();
mapFragment.setRetainInstance(true);
}
return mapFragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = inflater.inflate(R.layout.fragment_map, container, false);
supportMapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
if(supportMapFragment != null) {
if(supportMapFragment.getExtendedMap() != null) {
mCurrentMap = supportMapFragment.getExtendedMap();
}
}
setupMapIfNeeded();
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
private void setupMapIfNeeded() {
if(supportMapFragment == null) {
supportMapFragment = SupportMapFragment.newInstance();
supportMapFragment.setRetainInstance(true);
FragmentTransaction tx = getActivity().getSupportFragmentManager().beginTransaction();
tx.replace(R.id.map, supportMapFragment);
tx.commit();
}
if(mCurrentMap == null) {
supportMapFragment.getExtendedMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
mCurrentMap = googleMap;
setupMap();
}
});
}
}
public void setupMap() {
LatLng sydney = new LatLng(-33.867, 151.206);
mCurrentMap.setMyLocationEnabled(true);
mCurrentMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
mCurrentMap.addMarker(new MarkerOptions()
.title("Sydney")
.snippet("The most populous city in Australia.")
.position(sydney));
mCurrentMap.addMarker(new MarkerOptions().position(new LatLng(-23.5526, -46.6422)));
}
}
更新
现在,方向很好。 我将xml中的FrameLayout视图更改为:
<fragment
android:id="@+id/map"
class="com.androidmapsextensions.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
我删除了实例化supportMapFragment的代码段。