Adding a Listener to a Google Map

十年热恋 提交于 2019-12-11 00:38:00

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/21215572/adding-a-listener-to-a-google-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!