How to change Google Maps marker icon?

Zookey picture Zookey · Apr 27, 2017 · Viewed 18k times · Source

I want to change selected marker icon on Google Maps, so I have following code:

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

On this line I get error from below:

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

Here is the 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)

Answer

Gilad Brunfman picture Gilad Brunfman · Jun 27, 2017

This is how I set a drawable as a marker on 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);