DidSelectAnnotationView annotation tap not responding properly

时间:2018-01-01 02:06:48

标签: xamarin.ios mapkit mkannotationview

I have custom annotation which I have subClass from MKPointAnnotation. Adding those is working properly. I also need to detect the Annotation select method. The problem is when I tap on the annotation it doesn't hit the "DidSelectAnnotationView" at first. if I tap into another annotation like userLocation annotation then "DidSelectAnnotationView" hits. and while debugging it shows the coordinates of the annotation view is not the user location but the annotation I tap previously. and same happens after this when I tap my custom annotation it hits the method and coordinates of the method is not the userLocation one. I have added my code, could someone look into it where I missed the bits.

override public MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
    {
        string resuseId = "customAnnotation";

        MKAnnotationView annotationView = mapView.DequeueReusableAnnotation(resuseId);

        if (ThisIsTheCurrentLocation(mapView, annotation))
        {
            return null;
        }

        if (annotationView == null)
        {
            if (annotation is CustomAnnotation)
            {

                switch (CustomAnnotation.MarkerType)
                {
                    case MyMarkerType.Note:
                        annotationView = new MKAnnotationView(annotation, resuseId);
                        annotationView.Image = UIImage.FromBundle("Message");
                        annotationView.CanShowCallout = false;
                        annotationView.Enabled = true;
                        break;
                    case MyMarkerType.Photo:
                        annotationView = new MKAnnotationView(annotation, resuseId);
                        annotationView.Image = UIImage.FromBundle("Photo");
                        annotationView.CanShowCallout = false;
                        break;
                    case MyMarkerType.Story:
                        annotationView = new MKAnnotationView(annotation, resuseId);
                        var Img = UIImage.FromBundle("Story");
                        annotationView.CanShowCallout = false;
                        break;
                    case MyMarkerType.Custom:
                        annotationView = new MKAnnotationView(annotation, resuseId);
                        //using (var data = NSData.FromArray(CustomAnnotation.WayPoint.Image))
                        //{
                        //    var image = UIImage.LoadFromData(data);
                        //        annotationView.Image = image;
                        //}
                        NSData data = NSData.FromArray(CustomAnnotation.WayPoint.Image);
                        UIImage image = UIImage.LoadFromData(data);
                       // UIImage finalImage = image.MaxResizeImage(21f, 20f);
                        annotationView.Image = image;
                        annotationView.CanShowCallout = false;
                        break;
                    default:
                        annotationView = new MKAnnotationView(annotation, resuseId);
                        //var imaget = FromUrl(CustomAnnotation.WayPoint.IconUrl);
                        //annotationView.Image = imaget;
                        break;
                }
            }

            else{
                annotationView.Annotation = annotation;
                annotationView.CanShowCallout = false;

                //(annotationView as MKPinAnnotationView).AnimatesDrop = false; // Set to true if you want to animate the pin dropping
                //(annotationView as MKPinAnnotationView).PinTintColor = UIColor.Red;
                annotationView.SetSelected(false, false);
            }

        }

        return annotationView;

    }

And my DidSelect Method

public override void DidDeselectAnnotationView(MKMapView mapView, MKAnnotationView view)
    {

        if ( view.Annotation.Coordinate.Latitude == mapView.UserLocation.Coordinate.Latitude){

            return;
        }

        CLLocationCoordinate2D coordinates = view.Annotation.Coordinate;
        mapView.DeselectAnnotation(view.Annotation, false);
        // GetAnnotationClickInfo.Invoke(coordinates);


    }

2 个答案:

答案 0 :(得分:0)

检查代码后,我认为在初始化annotationView时出现错误,您应该将annotationView.Annotation = annotation;置于条件if (annotationView == null)之外。

Mofidy您的代码:

if (annotationView == null)
{
    if (annotation is CustomAnnotation)
    {
       //custom view logic
    }
    else  //not custom view
    {
        annotationView = new MKPinAnnotationView(annotation, annotationIdentifier);
    }
}
else
{
    annotationView.Annotation = annotation;
}

annotationView.CanShowCallout = false;
//(annotationView as MKPinAnnotationView).AnimatesDrop = false; // Set to true if you want to animate the pin dropping
//(annotationView as MKPinAnnotationView).PinTintColor = UIColor.Red;
annotationView.SetSelected(false, false);

答案 1 :(得分:0)

使用DidSelectAnnotationView方法,
不是DidDeselectAnnotationView

public override void DidSelectAnnotationView(MKMapView mapView, MKAnnotationView view)