web-dev-qa-db-fra.com

android MapView in Fragment

Je veux avoir MapView à l'intérieur de mon Fragment

Ceci est mon FragmentLayout fichier 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"
    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>

Ma AndroidManifest.xml fichier

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:Android="http://schemas.Android.com/apk/res/Android"
    package="a.b.c.d"
    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="a.b.c.d.permission.MAPS_RECEIVE"
        Android:protectionLevel="signature" />

    <uses-permission Android:name="a.b.c.d.permission.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>

et ma classe Fragment

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

lorsque je lance l'application, je ne vois aucune vue de carte dans mon fragment

26
user3833308

De l'exemple de Josh Holtz sur GitHub :

Vous devez ajouter MapView dans votre Layout comme

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

et implémentez votre Fragment comme

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();
  }

}
39
M D

Ajout à la réponse de M D:

De documentation :

Un GoogleMap doit être acquis à l'aide de getMapAsync (OnMapReadyCallback). MapView initialise automatiquement le système de cartes et la vue.

Selon cela, la façon la plus correcte d'initialiser GoogleMap est d'utiliser getMapAsync.

Notez que votre classe doit implémenter 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();
}
18
Florin

Au cas où quelqu'un chercherait une version Kotlin de MapView Fragment;)

class MapViewKotlinFragment : Fragment(), OnMapReadyCallback {

private var mMap: MapView? = 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"))
}
6
lomza

Assurez-vous que vous remplacez ou appelez OnDestroy View ainsi que OnDestroy. Quelque chose comme ...

    public override void OnDestroy()
    {            
        base.OnDestroy();
        MapView.OnDestroy();
        OnDestroyView();            
    }

    public override void OnDestroyView()
    {            
        base.OnDestroyView();
        mapReadyCallbackList.Clear();            
}
0
ComeIn