0

I am using Xamarin to render map with pins in Android. I am able Load the map and show custom Info window for my app, but I want a button or a link inside my Info window which when clicked do some computation and start a new activity.

This is what I am looking for: enter image description here

On click of links (link1 and link2) I would like to redirect user to new screen based on some value. My concern here is how can I get a click function for the links.

Also I want the Info Window to have rounded corners and have a pointer pointing to the pin.

Here is my code for above example:

Activity

public class MainActivity : Activity, IOnMapReadyCallback
{
    private GoogleMap GMap;
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        // Set our view from the "main" layout resource  
        SetContentView(Resource.Layout.Main);
        SetUpMap();
    }
    private void SetUpMap()
    {
        if (GMap == null)
        {
            FragmentManager.FindFragmentById<MapFragment>(Resource.Id.googlemap).GetMapAsync(this);
        }
    }
    public void OnMapReady(GoogleMap googleMap)
    {
        this.GMap = googleMap;
        GMap.UiSettings.ZoomControlsEnabled = true;
        LatLng latlng = new LatLng(Convert.ToDouble(15.354942), Convert.ToDouble(73.929845));
        MarkerOptions markerOpt = new MarkerOptions();
        markerOpt.SetPosition(latlng).SetTitle("test")
                  .SetSnippet("test")
                  .SetIcon(BitmapDescriptorFactory.DefaultMarker(BitmapDescriptorFactory.HueOrange));
        MapAdapter adapter = new MapAdapter(this);
        GMap.SetInfoWindowAdapter(adapter);
        GMap.AddMarker(markerOpt).ShowInfoWindow();
        GMap.MoveCamera(CameraUpdateFactory.NewLatLngZoom(
          new LatLng(15.354942, 73.929845), 20));
    }
}

Adapter

public class MapAdapter : Java.Lang.Object, GoogleMap.IInfoWindowAdapter
{
    private IOnTouchListener infoButtonListener;
    LayoutInflater inflater;
    private Activity context;

    public MapAdapter(Activity context)
    {
        this.context = context;
    }

    public View GetInfoContents(Marker marker)
    {

        return null;
    }

    public View GetInfoWindow(Marker marker)
    {
        View view = context.LayoutInflater.Inflate(Resource.Layout.CustomWindow, null);

        TextView Link1 = (TextView)view.FindViewById(Resource.Id.Link1);
        TextView Link2 = (TextView)view.FindViewById(Resource.Id.Link2);

        String udata = "Link1";
        SpannableString content = new SpannableString(udata);
        content.SetSpan(new UnderlineSpan(), 0, udata.Length, 0);
        gotoLink.SetText(content,TextView.BufferType.Normal);

        String udataPickup = "Link2";
        SpannableString contentPickup = new SpannableString(udataPickup);
        contentPickup.SetSpan(new UnderlineSpan(), 0, udataPickup.Length, 0);
        pickupLink.SetText(contentPickup, TextView.BufferType.Normal);
        return view;
    }
}
Arti
  • 2,993
  • 11
  • 68
  • 121

1 Answers1

0

Actually, it is not possible, Google Map renders the content of your custom InfoWindow into an image and displays it to you as an Image. Therefore you set a click Listener only to the whole window and not to the Views inside it.

Your only choice is to set the ClickListener to the whole InfoWindow and popup a Dialog with clickable content you want, and not do it directly inside the InfoWindow.

From Google Docs: https://developers.google.com/maps/documentation/android/marker#info_windows

Note: The info window that is drawn is not a live view. The view is rendered as an image (using View.draw(Canvas)) at the time it is returned. This means that any subsequent changes to the view will not be reflected by the info window on the map. To update the info window later (e.g., after an image has loaded), call showInfoWindow(). Furthermore, the info window will not respect any of the interactivity typical for a normal view such as touch or gesture events. However, you can listen to a generic click event on the whole info window as described in the section below.`

FreakyAli
  • 13,349
  • 3
  • 23
  • 63