Prashant Bhatt
Prashant Bhatt

Reputation: 11

How to handle Google Map marker click?

I have created Google Map with multiple marker dynamically using Database,now can anybody suggested me how to create click event each of marker individually?

Upvotes: 0

Views: 3524

Answers (5)

Juan Chavez
Juan Chavez

Reputation: 3

In order to attach unique URLs per marker click, you need to attach an object that you tag within Marker.Tag as follows:

UrlObject url = new UrlObject()
                {
                    Url = "enteryoururlhere"
                };
marker.Tag = UrlObject;

Then, you would get your URL that is unique to each marker by calling the following:

public boolean onMarkerClick(Marker marker) {
        UrlObject obj = marker.Tag;
        String activityUrl = obj.Url;
        Intent intent = new Intent(Intent.ACTION_VIEW, 
        Uri.parse(activityUrl));
        startActivity(intent);
   }

Your UrlObject class would be easy to setup:

    class UrlObject : Java.Lang.Object
    {
          public string Url { get; set; }
    }

Edit: You would make a tag for every marker in your loop. That way, when each marker is created, it is tagged properly with each url.

Upvotes: 0

Abdul Rahman K
Abdul Rahman K

Reputation: 664

I think the below approach may help you:

As others have said, you can set an OnMarkerClickListener to the marker, to perform an event on Marker click and add launch activity using an URL, refer here. So you can store the activity url also in DB. When dynamically creating the marker, maintain a Map whose key will be object of Marker class and Value will be String the URL.

so, inside onMarkerClick() method:

@Override
public boolean onMarkerClick(Marker marker) {
   String activityUrl = markerURLMap.get(marker);
   Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(activityUrl));
   startActivity(intent);
}

Upvotes: 0

FreakyAli
FreakyAli

Reputation: 16459

If you are looking for Xamarin code for the this, following is how you do it

GoogleMaps  Gmap;                                              

GMap.MarkerClick += GMap_MarkerClick;
GMap.InfoWindowClick += GMap_InfoWindowClick;

private void GMap_MarkerClick(object sender, GoogleMap.MarkerClickEventArgs e)
    {
        Marker marker = e.Marker;
        marker.ShowInfoWindow();
    }
private void GMap_InfoWindowClick(object sender, GoogleMap.InfoWindowClickEventArgs e)
    {

    }

this is how you handle both marker and marker info window click events in xamarin Android Hope this helps you.

Upvotes: 0

sumit
sumit

Reputation: 1087

Use this for Multiple markers:

First make your app to implement GoogleMap.OnMarkerClickListener Then create a Marker array :

Marker[] marker = new Marker[20]; //change length of array according to you

then inside

onMapReady(){
    mMap.setOnMarkerClickListener(this);
for(int i = 0 ; i < yourMarkerListsize ; i++ ) {
        marker[i] = mMap.addMarker(new MarkerOptions()
        .position(new LatLng(list(i).getLat(), list(i).getLon())
        .anchor(0.5f, 0.5f)
        .title(title)
        .snippet(snippet)
        .icon(yourIcon));
    }

then finally

@Override
    public boolean onMarkerClick(Marker marker) {
   //you can get assests of the clicked marker
   return false;
}

Hope it helps!!!

Upvotes: 0

Milind Mevada
Milind Mevada

Reputation: 3285

You can do like this

private Marker marker1;
    marker1 = mMap.addMarker(new MarkerOptions()
                    .position(LatLng(-31.952854, 115.857342))
                    .title("Title");
            marker1.setTag(0);

     mMap.setOnMarkerClickListener(this);

and in implementation of listener

 @Override
    public boolean onMarkerClick(final Marker marker) {
       //marker.getTag()
    }

Upvotes: 1

Related Questions