basically when adding a marker to the map it returns the new marker and you can gets the marker id from it like so
Marker m = map.addMarker(new MarkerOptions() .position(new LatLng(lat,lon))); String id = m.getId();
is there a way to get a marker by its id if there are many markers and you just want to delete one?
I have done this way:
Initialize variables:
private GoogleMap mMap; private HashMap<Marker, Integer> mHashMap = new HashMap<Marker, Integer>(); private ArrayList<MyCustomModelClass> myList = new ArrayList<MyCustomModelClass>();
Add marker on google map using your arraylist:
for (int i = 0; i < myList.size(); i++) { double latitude = myList.getLatitude(); double longitude = myList.getLongitude(); Marker marker = mMap.addMarker(new MarkerOptions().position(new LatLng(latitude,longitude))).title(myList.getTitle()) .icon(BitmapDescriptorFactory.fromResource(R.drawable.location_icon)); mHashMap.put(marker, i); }
On marker click listener:
@Override public boolean onMarkerClick(Marker marker) { int pos = mHashMap.get(marker); Log.i("Position of arraylist", pos+""); }
Hope this will help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With