ios - 如何为每个图钉设置不同的图像? Xamarin iOS

标签 ios xamarin xamarin.ios

我停在那里一会儿,我想知道有人可以帮助我。我们知道 MKAnnotationView.Image = some image,会将所有的 pin 设置为这张图片。如何为不同的引脚设置不同的图像。通常,有一个for循环将一个pin标记为一个图像,但是对于MKAnnotationView.Image,如何设置它。

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

  if (annotation is MKUserLocation)
    return null;

  var anno = annotation as MKPointAnnotation;
  var customPin = GetCustomPin (anno);
  if (customPin == null) {
    throw new Exception ("Custom pin not found");
  }

  annotationView = mapView.DequeueReusableAnnotation (customPin.Id);
  if (annotationView == null) {
    annotationView = new CustomMKAnnotationView (annotation, customPin.Id);
    annotationView.Image = UIImage.FromFile ("pin.png");
    annotationView.CalloutOffset = new CGPoint (0, 0);
    annotationView.LeftCalloutAccessoryView = new UIImageView (UIImage.FromFile ("monkey.png"));
    annotationView.RightCalloutAccessoryView = UIButton.FromType (UIButtonType.DetailDisclosure);
    ((CustomMKAnnotationView)annotationView).Id = customPin.Id;
    ((CustomMKAnnotationView)annotationView).Url = customPin.Url;
  }
  annotationView.CanShowCallout = true;

  return annotationView;
}

annotationView.Image = UIImage.FromFile ("pin.png");会将所有 pin 设置为此图像。但是,通常人们希望每个图钉都有不同的图像。任何想法,我真的很感激。

这是此代码的链接 https://github.com/xamarin/xamarin-forms-samples/blob/master/CustomRenderers/Map/iOS/CustomMapRenderer.cs

有官方公会自定义 map https://developer.xamarin.com/guides/xamarin-forms/custom-renderer/map/

最佳答案

这是我在 Xamarin 论坛中找到的内容。

annotationView.Image = UIImage.FromFile ("pin.png");

如果你有任何图像,只需将图像签名到 annotationView.Image 中。您可以将图像标识存储在我的自定义 pin 中。因此,您只需使用图像标识来指出您要使用的正确图像。

类似于 annotationView.Image = UIImage.FromFile (customPin.Image) 这是你想要的吗?

关于ios - 如何为每个图钉设置不同的图像? Xamarin iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40867783/

相关文章:

xamarin - 为什么不同平台无法绑定(bind)背景色?

ios - 如何在 Xamarin 中执行 UIAlertAction 的操作?

ios - Swift 在按下按钮并获取其 currentTitle 后返回 fatal error

iOS OpenGL ES 2.0 VBO 混淆

azure - MobileServiceClient 中未找到 LogoutAsync() 方法

iphone - iOS 5 单点触控 Collection View

c# - UIControllerView 无法从委托(delegate)函数更新

ios - 在编译时将大型数据库文件添加到 iOS App bundle 以避免索引?

ios - Swift - UIButton 没有固定在屏幕底部

c# - Xamarin-MediaPlugin保存到相册在iOS下不起作用