Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
873 views
in Technique[技术] by (71.8m points)

android - Adding a Listener to a Google Map

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...