Reputation: 249
how to find which marker is selected in the list of markers in google map from onMarkerClicked(Marker marker) callback.
Upvotes: 4
Views: 4049
Reputation: 18871
You can do this via marker.setTag(MyMarkerTagObject)
and marker.getTag(MyMarkerTagObject)
.
You'll just need to create a plain MyMarkerTagObject
class that contains your id
field (along with any other fields you would like to add) with setter/getter methods for those fields. For example:
MyMarkerTagObject myMarkerTagObject = new MyMarkerTagObject();
myMarkerTagObject.setId(someId);
marker.setTag(myMarkerTagObject);
And on the other side of things:
MyMarkerTagObject myMarkerTagObject = (MyMarkerTagObject)marker.getTag();
long sameId = myMarkerTagObject.getId();
Upvotes: 0
Reputation: 11545
You can get lat, long, marker id, etc of a selected marker from onMarkerClick(Marker marker)
like :
@Override
public boolean onMarkerClick(Marker marker) {
Log.i("GoogleMapActivity", "onMarkerClick");
LatLng position = marker.getPosition();
String idSelected= marker.getId();
/* Toast.makeText(getApplicationContext(),
"Marker Clicked: " + marker.getTitle(), Toast.LENGTH_LONG)
.show();*/
return false;
}
Upvotes: 0
Reputation: 1367
The recommended way to do this is to have a Hash with marker ID and your custom data. The Marker object might change if the activity is killed and restored but the ID will remain the same. You map would look like:
HashMap<String, MyObject> markersAndObjects = new HashMap<String, MyObject>();
Marker objects have a getId() method to get the ID. Hope it helps.
Upvotes: 2
Reputation:
You can identify by id or title.
@Override
public boolean onMarkerClick(Marker marker) {
marker.getId()
marker.getTitle();
});
Or you can extend Marker and in constructor pass every data which you want, and in onMarkerClick cast to your own Marker and get data.
Upvotes: -1