2016-08-21 51 views
1

我正在Xamarin Forms中开发一个应用程序,该应用程序具有显示地图上地点的徽标的自定义地图。所有的运作都很好,除了如果我玩一下地图(例如移动到另一个位置或转动地图并返回),我可以看到这些引脚交换了他们的位置。 (见下面的图片)iOS地图中的引脚在玩地图后互相交换图像(Xamarin)

This is their initial positions

This is what they look like after manipulation

任何想法,为什么出现这种情况?

+0

你可以告诉你如何创建销? –

+0

@ Sven-MichaelStübe,我自己解决了,谢谢你的回复! –

回答

0

我自己解决了。 事实证明,mapView.DequeueReusableAnnotation函数每次调用重绘引脚时都会返回错误的注释。 我删除了该功能,现在每次绘制引脚时都会创建新的注释。

这里是方法的代码,如果有人面临着同样的问题:

MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation) 
    { 
     MKAnnotationView annotationView = null; 

     if (annotation is MKUserLocation) 
      return null; 

     var anno = annotation as MKPointAnnotation; 

     if (anno == null) return null; 

     var customPin = GetCustomPin(anno); 
     if (customPin == null) 
     { 
      throw new Exception("Custom pin not found"); 
     } 
     // commented the following line: 
     //annotationView = mapView.DequeueReusableAnnotation(customPin.Id); 
     //if (annotationView == null) 
     //{ 
     annotationView = new CustomMKPinAnnotationView(annotation, customPin.Id); 

     byte[] resizedLogo = ImageResizer.ResizeImage(customPin.Logo, 32, 32); 
     var data = NSData.FromArray(resizedLogo); 
     annotationView.Image = UIImage.LoadFromData(data); 
     annotationView.CalloutOffset = new CGPoint(0, 0); 
     annotationView.LeftCalloutAccessoryView = new UIImageView(UIImage.FromFile("logo40.png")); 
     annotationView.RightCalloutAccessoryView = UIButton.FromType(UIButtonType.DetailDisclosure); 
     ((CustomMKPinAnnotationView)annotationView).Id = customPin.Id; 
     ((CustomMKPinAnnotationView)annotationView).RestID = customPin.RestID; 
     //} 
     annotationView.CanShowCallout = true; 

     return annotationView; 
    } 
+0

但是,您始终都在创造新的视图,这是昂贵而无用的。 'DequeueReusableAnnotation'使用提供的ID来重新使用视图。如果你想以正确的方式解决这个问题,你应该让你的ID取决于你想要显示的图像。你的问题是'DequeueReusableAnnotation'误用/误解的典型特征。 –