web-dev-qa-db-fra.com

Comment changer l'icône du marqueur Google Maps?

Je souhaite modifier l'icône de marqueur sélectionnée sur Google Maps, j'ai donc le code suivant:

googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {
            if (null != currentMarker) {
                currentMarker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.ic_arrowautougasen));
            }
            currentMarker = marker;
            currentMarker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.ic_autoselektovan));              
            return true;
        }
    });

 googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
        @Override
        public void onMapClick(LatLng latLng) {
            if(null != currentMarker) {
                currentMarker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.ic_arrowautougasen));
            }
            currentMarker = null;
        }
    });

Sur cette ligne, j'obtiens une erreur ci-dessous:

       if(null != currentMarker) {
                currentMarker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.ic_arrowautougasen));
            }

Voici l'exception:

 Java.lang.IllegalArgumentException: Unmanaged descriptor
                                                                    at com.google.maps.api.Android.lib6.common.k.b(:com.google.Android.gms.DynamiteModulesB:162)
                                                                    at com.google.maps.api.Android.lib6.impl.o.c(:com.google.Android.gms.DynamiteModulesB:75)
                                                                    at com.google.maps.api.Android.lib6.impl.db.a(:com.google.Android.gms.DynamiteModulesB:334)
                                                                    at com.google.Android.gms.maps.model.internal.q.onTransact(:com.google.Android.gms.DynamiteModulesB:204)
                                                                    at Android.os.Binder.transact(Binder.Java:387)
                                                                    at com.google.Android.gms.maps.model.internal.zzf$zza$zza.zzL(Unknown Source)
                                                                    at com.google.Android.gms.maps.model.Marker.setIcon(Unknown Source)
                                                                    at ba.kordinata.kogps.live.map.MapFragment$5.onMapClick(MapFragment.Java:212)
7
Zookey

Voici comment définir un drawable comme marker sur Google Maps:

    mMap = googleMap;

    BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.your_drawable_here);
    LatLng sydney = new LatLng(-33.852, 151.211);
    MarkerOptions markerOptions = new MarkerOptions().position(sydney)
            .title("Marker in Sydney")
            .snippet("snippet snippet snippet snippet snippet...")
            .icon(icon);
    mMap.addMarker(markerOptions);
7
Gilad Brunfman