Set Custom Marker Icon at Current Location - Android API v2

Gaurav Arora picture Gaurav Arora · Jul 15, 2013 · Viewed 20.2k times · Source

I want to show a marker of my own choice in place of that default blue colored icon. How can I change the same.

Currently I am adding it manually at current location.

@Override
            public void onMyLocationChange(Location location) {
                // Creating a LatLng object for the current location
                LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
                // Showing the current location in Google Map
                CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(
                        latLng, 15);
                googleMap.animateCamera(cameraUpdate);
                marker = googleMap.addMarker(new MarkerOptions()
                .position(latLng)
                .title("Current Location(You)")
                .snippet("Current")
                .icon(BitmapDescriptorFactory
                        .fromResource(R.drawable.green_loc_icon))
                        .draggable(true));
            }
        });

Answer

fasteque picture fasteque · Aug 25, 2013

It's explained in the documentation:

https://developers.google.com/maps/documentation/android/marker#change_the_default_marker

Please remember to remove the marker before adding a new one, otherwise each time you get an update for the user location you'll add a new marker on the map.

Since probably you're showing other markers on the map, hold a reference to the user current location marker and then use remove() method before creating a new instance.