Android MapView in Fragment

Ich möchte MapView in meinem Fragment

Dies ist meine FragmentLayout XML-Datei

 <?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" android:background="#17df0d" android:orientation="vertical" > <com.google.android.gms.maps.MapView android:id="@+id/mapview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/textView1" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_marginBottom="70dp" > </com.google.android.gms.maps.MapView> </RelativeLayout> 

Meine AndroidManifest.xml Datei

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="abcd" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="your_api_key" /> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> </application> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <permission android:name="abcdpermission.MAPS_RECEIVE" android:protectionLevel="signature" /> <uses-permission android:name="abcdpermission.MAPS_RECEIVE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> </manifest> 

Und meine Fragmentklasse

 public class ReportFragment extends Fragment implements LocationListener { MapView mapView = null; //eventually it is being read from view and assigned 

Wenn ich die App lasse, sehe ich keine Kartenansicht in meinem Fragment

  • Holen Sie sich Google Maps, um automatisch auf meinen aktuellen Standort zu zoomen?
  • Sqlite-Datenbank - Wie zu speichern Bild aus der Kamera-Absicht sowie Hashmap zu sqlite, um später abzurufen?
  • Android Maps: Fehler beim Laden der Karte. Google-Server konnte nicht kontaktiert werden
  • Google Maps API v2: Wie kann man Markierungen anklicken?
  • MapView v2 halten Kontext um
  • Drehen Sie den Marker auf der Grundlage der Fahrtrichtung
  • Android - Fehler beim Finden des Stils 'mapViewStyle' im aktuellen Thema
  • Android || Holen Sie sich aktuelle Google Maps Route Information
  • 3 Solutions collect form web for “Android MapView in Fragment”

    Von Josh Holtzs Beispiel auf GitHub :

    Sie sollten MapView in Ihrem Layout wie hinzufügen

      <com.google.android.gms.maps.MapView android:id="@+id/mapview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> 

    Und implementiere dein Fragment wie

     public class SomeFragment extends Fragment { MapView mapView; GoogleMap map; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.some_layout, container, false); // Gets the MapView from the XML layout and creates it mapView = (MapView) v.findViewById(R.id.mapview); mapView.onCreate(savedInstanceState); // 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(43.1, -87.9), 10); map.animateCamera(cameraUpdate); return v; } @Override public void onResume() { mapView.onResume(); super.onResume(); } @Override public void onDestroy() { super.onDestroy(); mapView.onDestroy(); } @Override public void onLowMemory() { super.onLowMemory(); mapView.onLowMemory(); } } 

    Hinzufügen zur Antwort von MD:

    Aus Dokumentation :

    Eine GoogleMap muss mit getMapAsync (OnMapReadyCallback) erworben werden. Das MapView initialisiert automatisch das Kartensystem und die Ansicht.

    Dementsprechend ist die korrekte Art, GoogleMap zu initialisieren, mit getMapAsync .

    Beachten Sie, dass Ihre Klasse OnMapReadyCallback implementieren OnMapReadyCallback

     @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_map_page, container, false); mMapView = (MapView) v.findViewById(R.id.map_view); mMapView.onCreate(savedInstanceState); mMapView.getMapAsync(this); //this is important return v; } @Override public void onMapReady(GoogleMap googleMap) { mGoogleMap = googleMap; mGoogleMap.getUiSettings().setZoomControlsEnabled(true); mGoogleMap.addMarker(new MarkerOptions().position(/*some location*/)); mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(/*some location*/, 10)); } @Override public void onResume() { super.onResume(); mMapView.onResume(); } @Override public void onPause() { super.onPause(); mMapView.onPause(); } @Override public void onDestroy() { super.onDestroy(); mMapView.onDestroy(); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mMapView.onSaveInstanceState(outState); } @Override public void onLowMemory() { super.onLowMemory(); mMapView.onLowMemory(); } 

    Falls jemand nach einer Kotlin-Version von MapView Fragment sucht;)

     class MapViewKotlinFragment : Fragment(), OnMapReadyCallback { private var mMap: GoogleMap? = null override fun onSaveInstanceState(outState: Bundle?) { super.onSaveInstanceState(outState) mMap?.onSaveInstanceState(outState) } override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { val view = inflater?.inflate(R.layout.fragment_map, container, false) mMap = view?.findViewById(R.id.mapViewPlaces) as MapView mMap?.onCreate(savedInstanceState) mMap?.getMapAsync(this) return view } override fun onResume() { super.onResume() mMap?.onResume() } override fun onPause() { super.onPause() mMap?.onPause() } override fun onStart() { super.onStart() mMap?.onStart() } override fun onStop() { super.onStop() mMap?.onStop() } override fun onDestroy() { super.onDestroy() mMap?.onDestroy() } override fun onLowMemory() { super.onLowMemory() mMap?.onLowMemory() } override fun onMapReady(googleMap: GoogleMap) { googleMap.addMarker(MarkerOptions().position(LatLng(0.0, 0.0)).title("Marker")) } 
    Das Android ist ein Google Android Fan-Website, Alles ├╝ber Android Phones, Android Wear, Android Dev und Android Spiele Apps und so weiter.