我正在Xamarin Forms中開發一個應用程序,該應用程序具有顯示地圖上地點的徽標的自定義地圖。所有的運作都很好,除了如果我玩一下地圖(例如移動到另一個位置或轉動地圖並返回),我可以看到這些引腳交換了他們的位置。 (見下面的圖片)iOS地圖中的引腳在玩地圖後互相交換圖像(Xamarin)
This is their initial positions
This is what they look like after manipulation
任何想法,爲什麼出現這種情況?
我正在Xamarin Forms中開發一個應用程序,該應用程序具有顯示地圖上地點的徽標的自定義地圖。所有的運作都很好,除了如果我玩一下地圖(例如移動到另一個位置或轉動地圖並返回),我可以看到這些引腳交換了他們的位置。 (見下面的圖片)iOS地圖中的引腳在玩地圖後互相交換圖像(Xamarin)
This is their initial positions
This is what they look like after manipulation
任何想法,爲什麼出現這種情況?
我自己解決了。 事實證明,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;
}
但是,您始終都在創造新的視圖,這是昂貴而無用的。 'DequeueReusableAnnotation'使用提供的ID來重新使用視圖。如果你想以正確的方式解決這個問題,你應該讓你的ID取決於你想要顯示的圖像。你的問題是'DequeueReusableAnnotation'誤用/誤解的典型特徵。 –
你可以告訴你如何創建銷? –
@ Sven-MichaelStübe,我自己解決了,謝謝你的回覆! –