MapView (Android Maps API v2) внутри макета фрагмента не показывает
Я использую viewpager для пролистывания между двумя вкладками панели действий (для этого вида навигации используется мастер затмения). Я использую Android карты V2 API.
Я хочу иметь вид карты, кнопку и текстовый вид внутри одной из моих вкладок (я думаю, что иметь фрагмент карты невозможно).
Я раздуваю макет для моего фрагмента из XML следующим образом:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.first_tab_fragment, container, false);
}
}
Мой первый_таб_фрагмент.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.google.android.gms.maps.MapView
android:id="@+id/mapview"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/textView1"
android:layout_marginRight="15dp" >
</com.google.android.gms.maps.MapView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView1"
android:layout_centerHorizontal="true"
android:text="Button" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/button1"
android:layout_marginBottom="133dp"
android:text="TextView" />
MapView не отображается, только текстовое представление и кнопка. Я тоже не получаю ошибок. Должен ли я написать код для инициализации mapview?
1 ответ
Я решил это, добавив этот код в onCreateView:
// Gets the MapView from the XML layout and creates it
mapView = (MapView) v.findViewById(R.id.mapview);
mapView.onCreate(savedInstanceState);
mapView.onResume(); //without this, map showed but was empty
// Gets to GoogleMap from the MapView and does initialization stuff
map = mapView.getMap();
map.getUiSettings().setMyLocationButtonEnabled(false);
map.setMyLocationEnabled(true);
// Needs to call MapsInitializer before doing any CameraUpdateFactory calls
try {
MapsInitializer.initialize(this.getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
// Updates the location and zoom of the MapView
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(44.14, 14.2), 10);
map.animateCamera(cameraUpdate);