Skip to content Skip to sidebar Skip to footer

How To Get Id Of Marker Clicked In Google Map Inside Setoninfowindowclicklistener

Here what I am trying to do is set Id inside doInBackground and call it in setOnInfoWindowClickListener.But the problem is that it is getting the Id of the first element in the lis

Solution 1:

Add the product.id as a tag for your Marker:

Markermarker= googleMap.addMarker(newMarkerOptions()
        .position(newLatLng(product.lat, product.lon))
        .title(product.name)
        .icon(BitmapDescriptorFactory.fromBitmap(product.icon)));
marker.setTag(product.id);

And then on your OnInfoWindowClickListener get the tag:

googlemap.setOnInfoWindowClickListener(newOnInfoWindowClickListener() {
    @OverridepublicvoidonInfoWindowClick(Marker marker) {
        intent.putExtra("PRODUCT_ID",(String) marker.getTag());
        startActivity(intent);
    }
}

Another option could be to keep a Map<Marker, String> to store the id associated to each Marker:

privateMap<Marker, String> markerIds = newHashMap<>();

You will need to create your markers and store them like this:

Markermarker= googleMap.addMarker(newMarkerOptions().position(markerPosition).title("my marker"));
markerIds.put(marker, product.id);

And then on your OnInfoWindowClickListener get the tag:

googlemap.setOnInfoWindowClickListener(newOnInfoWindowClickListener() {
    @OverridepublicvoidonInfoWindowClick(Marker marker) {
        intent.putExtra("PRODUCT_ID",markerIds.get(marker));
        startActivity(intent);
    }
}

Post a Comment for "How To Get Id Of Marker Clicked In Google Map Inside Setoninfowindowclicklistener"