Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a map marker by its ID in Google Maps v2

Tags:

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?

like image 348
tyczj Avatar asked Mar 12 '13 17:03

tyczj


1 Answers

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.

like image 137
Hiren Patel Avatar answered Sep 19 '22 12:09

Hiren Patel