Warm tip: This article is reproduced from serverfault.com, please click

Adding a Listener to a Google Map

发布于 2014-01-19 10:28:14

In Xamarin, how do I add an OnInfoWindowClickListener to a Google map?

I have read that:

You can use an OnInfoWindowClickListener to listen to click events on an info window. To set this listener on the map, call GoogleMap.setOnInfoWindowClickListener(OnInfoWindowClickListener). When a user clicks on an info window, onInfoWindowClick(Marker) will be called

The resource is here: https://developers.google.com/maps/documentation/android/infowindows

This is the interface code I have added:

public interface onInfoWindowClickListener {
    void onInfoWindowClick(Marker marker);
}

I have added this method:

public void onInfoWindowClick(Marker marker)
{
    //Toast.MakeText (MapWithMarkersActivity.this, String.Format ("InfoWindow is Clicked"), ToastLength.Short).Show ();
}

I am getting an error at this line of code:

_map.SetOnInfoWindowClickListener (new onInfoWindowClickListener());

The error is this:

Cannot create an instance of the abstract class or interface 'onInfoWindowClickListener'

Can I please have some help?

Thanks in advance

Questioner
user22707
Viewed
0
Mike Stonis 2014-01-20 00:49:18

Take a look at the Xamarin Docs for Google Maps. This should show you exactly how to setup your click listener.

Here is the code that you would want to implement to get your info window click handled properly. the key line is _map.InfoWindowClick += MapOnInfoWindowClick;.

private bool SetupMapIfNeeded()
{
    if (_map == null)
    {
        _map = _mapFragment.Map;
        if (_map != null)
        {
            _map.InfoWindowClick += MapOnInfoWindowClick;
            return true;
        }
        return false;
    }
    return true;
}


private void MapOnInfoWindowClick (object sender, GoogleMap.InfoWindowClickEventArgs e)
{
    Marker myMarker = e.P0;
    // Do something with marker.
}