Prevent Annotationview closing

EsegeEsege Member ✭✭

When i tap on map naturally selected annotation's annotationview vanishes. Is there a way to prevent this?

Tagged:

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    Create a custom mapview and override the PointInside like:

    public class CustomMapView : MKMapView
    {
        bool allowSelectionChanges = true;
        public override bool GestureRecognizerShouldBegin(UIGestureRecognizer gestureRecognizer)
        {
            return allowSelectionChanges;
        }
    
        public override bool PointInside(CGPoint point, UIEvent uievent)
        {
            var pointInside = base.PointInside(point, uievent);
            if (!pointInside)
            {
                return pointInside;
            }
    
            foreach(var annotation in Annotations)
            {
                var view = ViewForAnnotation(annotation);
                if (view != null)
                {
                    if (view.Frame.Contains(point))
                    {
                        allowSelectionChanges = true;
                        return true;
                    }
                }
            }
            allowSelectionChanges = false;
    
            return pointInside;
        }
    }
    
  • EsegeEsege Member ✭✭

    @LandLu said:
    Create a custom mapview and override the PointInside like:

    public class CustomMapView : MKMapView
    {
        bool allowSelectionChanges = true;
        public override bool GestureRecognizerShouldBegin(UIGestureRecognizer gestureRecognizer)
        {
            return allowSelectionChanges;
        }
    
        public override bool PointInside(CGPoint point, UIEvent uievent)
        {
            var pointInside = base.PointInside(point, uievent);
            if (!pointInside)
            {
                return pointInside;
            }
    
            foreach(var annotation in Annotations)
            {
                var view = ViewForAnnotation(annotation);
                if (view != null)
                {
                    if (view.Frame.Contains(point))
                    {
                        allowSelectionChanges = true;
                        return true;
                    }
                }
            }
            allowSelectionChanges = false;
    
            return pointInside;
        }
    }
    

    PointInside is called but GestureRecognizerShouldBegin is not being called in my custom ios map renderer. How can i fix this?

  • LandLuLandLu Member, Xamarin Team Xamurai

    Do you mean you want to achieve it on Forms? If so, you need to instantiate the CustomMapView and then replace the native control with the instance.
    BTW, may I know which renderer you are using? Xamarin.Essentials.Map is not able to add annotation or other views on map as I know.

Sign In or Register to comment.