我有四种类型的自定义注释。我是PointAnnotation的子类,它将定义注释的枚举类型。自定义类型是我从后端获得的注释,并动态添加它们。添加注释很好。但是当我移动地图时,注释的图像会发生变化,我也注意到它改变了注释的类型。
我的代码是:
override public MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
{
string resuseId = "";
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 = "Note");
annotationView.Image = UIImage.FromBundle("Message");
annotationView.UserInteractionEnabled = true;
// CGPointMake(0, -imageHeight / 2).
annotationView.CanShowCallout = false;
break;
case MyMarkerType.Photo:
annotationView = new MKAnnotationView(annotation, resuseId = "Photo");
annotationView.Image = UIImage.FromBundle("Photo");
annotationView.CanShowCallout = false;
break;
case MyMarkerType.Story:
annotationView = new MKAnnotationView(annotation, resuseId = "Story");
annotationView.Image = UIImage.FromBundle("Story");
annotationView.CanShowCallout = false;
break;
case MyMarkerType.Custom:
annotationView = new MKAnnotationView(annotation, resuseId = "Custom");
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.Annotation = annotation;
break;
}
}
else //not custom view
{
annotationView = new MKMarkerAnnotationView(annotation, "cluster");
}
}
else
{
annotationView.Annotation = annotation;
}
annotationView.CanShowCallout = false;
return annotationView;
}
我看看这个答案MKAnnotationView image changing issue 但是在我的情况下无法弄清楚。
在重用annotationView时你做得不对,我们应该使用不同的resuseId
来分类custom view
和default view
。
override public MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
{
MKAnnotationView annotationView = null;
if (ThisIsTheCurrentLocation(mapView, annotation))
{
return null;
}
if (annotation is CustomAnnotation) //custom view
{
annotationView = mapView.DequeueReusableAnnotation("CustomID");
if (annotationView == null)
{
annotationView = new MKAnnotationView(annotation, resuseId = "CustomID");
}
switch (CustomAnnotation.MarkerType)
{
case MyMarkerType.Note:
annotationView.Image = UIImage.FromBundle("Message");
annotationView.UserInteractionEnabled = true;
break;
case MyMarkerType.Photo:
annotationView.Image = UIImage.FromBundle("Photo");
break;
case MyMarkerType.Story:
annotationView.Image = UIImage.FromBundle("Story");
break;
case MyMarkerType.Custom:
NSData data = NSData.FromArray(CustomAnnotation.WayPoint.Image);
UIImage image = UIImage.LoadFromData(data);
// UIImage finalImage = image.MaxResizeImage(21f, 20f);
annotationView.Image = image;
break;
default:
annotationView.Annotation = annotation;
break;
}
annotationView.Annotation = annotation;
}
else //not custom view
{
annotationView = mapView.DequeueReusableAnnotation("DefaultID");
if(annotationView == null)
{
annotationView = new MKMarkerAnnotationView(annotation, "DefaultID");
}
annotationView.Annotation = annotation;
}
annotationView.CanShowCallout = false;
return annotationView;
}