3

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));
            }
        });
Gaurav Arora
  • 8,282
  • 21
  • 88
  • 143

2 Answers2

1

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.

fasteque
  • 4,309
  • 8
  • 38
  • 50
-2

Change your code like this, it will work properly, include below permissions in AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="com.example.towntour.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

Code for plotting a pin:

public void onMyLocationChange(Location location) 
{

LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.title("Town Tour");
markerOptions.snippet("We are here");


find_us_map.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, 4));
find_us_map.animateCamera(CameraUpdateFactory.zoomTo(4), 2000, null);

markerOptions.position(latlng);
Marker m = find_us_map.addMarker(markerOptions);
}
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
Teraiya Mayur
  • 1,094
  • 10
  • 18
  • Let me check and get back to you.. Thanks – Gaurav Arora Jul 15 '13 at 12:44
  • he needs to set a custom marker icon, and thats what he is trying to to and did not succeed in his code. Now altough the code you wrote is correct, you did not change the default animation of the marker to a custom one, so basically your code is off topic. @ Gaurav, I have the same issue and BitmapDescriptorFactory doesn't want to change my icon, as soon as I find an answer will let you know – rosu alin Jun 16 '14 at 10:38