我有自定义注释,我有来自MKPointAnnotation的subClass。添加它们正常工作。我还需要检测Annotation select方法。问题是,当我点击注释时,它首先没有点击“DidSelectAnnotationView”。如果我点击另一个注释,如userLocation注释,然后“DidSelectAnnotationView”命中。并且在调试时显示注释视图的坐标不是用户位置,而是我之前点击的注释。并且在此之后发生同样的情况,当我点击我的自定义注释它命中方法时,方法的坐标不是userLocation。我已经添加了我的代码,有人可能会在我错过了这些内容的地方。
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;
}
还有我的DidSelect方法
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);
}
使用DidSelectAnnotationView
方法,
不是DidDeselectAnnotationView
。
public override void DidSelectAnnotationView(MKMapView mapView, MKAnnotationView view)
检查你的代码后,我认为在初始化annotationView
时有一个错误,你应该把annotationView.Annotation = annotation;
放在条件if (annotationView == null)
之外。
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);