Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
879 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...