How to change Google Maps marker icon?
Asked Answered
K

4

9

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)
Keeper answered 27/4, 2017 at 12:48 Comment(2)
Have you checked this answerCadge
https://mcmap.net/q/203064/-custom-marker-in-google-maps-in-android-with-vector-asset-icon worked for my project. I had a vector asset. so, converted it to bitmap and added the icon as markerThirzia
P
11

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);
Percept answered 27/6, 2017 at 17:25 Comment(1)
This creates a new icon. The OP wants to know how to change an existing markerBalas
C
1
final MarkerOptions marker=new MarkerOptions().position(loc);

marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker4));
mMap.addMarker(marker);
Coleman answered 3/7, 2020 at 13:7 Comment(0)
P
-2
double latitude = 12.5044;
double longitude = 60.6671;
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("MyMaps");
marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.my_icon)));
googleMap.addMarker(marker);
Pender answered 27/4, 2017 at 12:53 Comment(1)
This creates a new icon. The OP wants to know how to change an existing marker.Balas
D
-2
    public void onLocationChanged(Location location) {
        mLastLocation = location;
        if (currentLocationMarker != null) {
            currentLocationMarker.remove();
        }

        Log.d(Constant.TAG, "Location Changed");

        // resize bitmap
        //BitmapFactory.Options options = new BitmapFactory.Options();
        //options.inScaled = false;
        //currentBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.currentpos, options);
        //currentBitmap = SubUtils.scaleBitmap(currentBitmap, 60, 60);

        //Place current location marker
        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
        //create marker
        MarkerOptions markerOptions = new MarkerOptions();
        //add position to marker
        markerOptions.position(latLng);
        //change icon marker
        markerOptions.icon(BitmapDescriptorFactory.fromBitmap(currentBitmap));
        //add marker to googleMap
        currentLocationMarker = googleMap.addMarker(markerOptions);

        // animate targer marker
        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(latLng)
                .zoom(15)
                .build();
        //move the map to selected marker
        googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    }
Downward answered 27/4, 2017 at 14:44 Comment(1)
This creates a new icon. The OP wants to know how to change an existing marker.Balas

© 2022 - 2024 — McMap. All rights reserved.