Forum Xamarin Xamarin.Forms

Info Window is not displayed on click of Custom Marker (PIN) in Xamarin.Forms Shell Map

RakeshKumarKhandelwalRakeshKumarKhandelwal INMember ✭✭
edited March 23 in Xamarin.Forms

Hi,

I have created custom map using Xamarin.Forms Shell. I have created following renderer for customizing the pin image.

public class CustomMapRenderer : MapRenderer, GoogleMap.IInfoWindowAdapter
    {

    bool isDrawn;
        List<CustomPin> customPins;
        Android.Gms.Maps.GoogleMap map;

public CustomMapRenderer(Context context) : base(context)
        {
        }

 protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Map> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null)
            {
                NativeMap.InfoWindowClick -= OnInfoWindowClick;
            }

            if (e.NewElement != null)
            {
                var formsMap = (CustomMap)e.NewElement;
                customPins = formsMap.CustomPins;
            }
        }

protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
            var formsMap = sender as CustomMap;
            if (formsMap != null && formsMap.CustomPins != null && formsMap.CustomPins.Count > 0)
            {

                if (e.PropertyName.Equals("VisibleRegion") && !isDrawn)
                {
                    // Code to Update Pins image
                }
            }
        }

protected override void OnLayout(bool changed, int l, int t, int r, int b)
        {
            base.OnLayout(changed, l, t, r, b);

            if (changed)
            {
                isDrawn = false;
            }
        }
protected override void OnMapReady(Android.Gms.Maps.GoogleMap map)
        {
            base.OnMapReady(map);
            this.map = map;
            NativeMap.InfoWindowClick += OnInfoWindowClick;
            NativeMap.SetInfoWindowAdapter(this);

        }

protected override MarkerOptions CreateMarker(Pin pin)
        {
            //Create Marker Pin and return marker;
        }

void OnInfoWindowClick(object sender, GoogleMap.InfoWindowClickEventArgs e)
        {
        // This code is not being called I dont know why
            var customPin = GetCustomPin(e.Marker);
            if (customPin == null)
            {
                throw new Exception("Custom pin not found");
            }

        }

public Android.Views.View GetInfoContents(Marker marker)
        {
        // This code is not being called I dont know why
            var inflater = Android.App.Application.Context.GetSystemService(Context.LayoutInflaterService) as Android.Views.LayoutInflater;
            if (inflater != null)
            {
                Android.Views.View view;

                var customPin = GetCustomPin(marker);
                if (customPin == null)
                {
                    //throw new Exception("Custom pin not found");
                }

                view = inflater.Inflate(Resource.Layout.MapInfoWindow, null);

                var infoTitle = view.FindViewById<TextView>(Resource.Id.InfoWindowTitle);
                var infoSubtitle = view.FindViewById<TextView>(Resource.Id.InfoWindowSubtitle);

                if (infoTitle != null)
                {
                    infoTitle.Text = marker.Title;
                }
                if (infoSubtitle != null)
                {
                    infoSubtitle.Text = marker.Snippet;
                }

                return view;
            }
            return null;
        }

public Android.Views.View GetInfoWindow(Marker marker)
        {
    // Not Called in debug
            return null;
        }


        CustomPin GetCustomPin(Marker annotation)
        {
    // Not Called in debug
            var position = new Position(annotation.Position.Latitude, annotation.Position.Longitude);
            foreach (var pin in customPins)
            {
                if (pin.Position == position)
                {
                    return pin;
                }
            }
            return null;
        }
}

I am not able to see Info Window while clicking on custom marker. Can anyone help me what I am doing wring? Here when debugging the code, I am not getting called following functions / events:
GetCustomPin(Marker annotation)
public Android.Views.View GetInfoWindow(Marker marker)
public Android.Views.View GetInfoContents(Marker marker)

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    If it is convenient for you, could you please post a basic demo so that we can test on our side?

Sign In or Register to comment.