ios - Swift 注解自定义镜像

标签 ios swift xcode swift3

我正在使用 Swift 3 和 Xcode 10 beta 3,我需要为 map 上的图钉使用自定义图像。在另一篇文章中的一个人的帮助下,我们编写了这段代码,除了引脚仍然是默认值之外,一切正常。

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if let annotation = annotation as? Annotation {
            let identifier = "identifier"
            let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            annotationView.image = annotation.image //add this
            annotationView.canShowCallout = true
            annotationView.calloutOffset = CGPoint(x: -5, y: 5)
            annotationView.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) as UIView
            return annotationView
        }
        return nil
    }

    let marker = Annotation(title: "LVR" , subtitle: "" , coordinate: CLLocationCoordinate2D(latitude: 43.772523, longitude: 11.254356))
    marker.image = UIImage(named: "antonioli.png")
    //mapView.addAnnotation(marker)
    self.map.addAnnotation(marker)

PNG 文件位于我的项目的主文件夹中,如下所示: Description

我该如何处理这个问题?

最佳答案

这里MyAnnotationNSObject、MKAnnotation的子类

func mapView(_ mapView: MKMapView,viewFor annotation: MKAnnotation) -> MKAnnotationView?
{
    if annotation is MyAnnotation == false
    {

        return nil
    }

    let senderAnnotation = annotation as! MyAnnotation

    let pinReusableIdentifier = senderAnnotation.pinColor.rawValue

    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: pinReusableIdentifier)

    if annotationView == nil
    {
        annotationView = MKAnnotationView(annotation: senderAnnotation,                                                                                                reuseIdentifier: pinReusableIdentifier)
        annotationView!.canShowCallout = true

    }
    let pinImage = UIImage(named:"location_curr.png")

    annotationView!.image = pinImage

    return annotationView

}

查看演示 MapCustomImage

关于ios - Swift 注解自定义镜像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51243641/

相关文章:

ios - 看起来我们缺少 dSYM 来处理版本崩溃

ios - UIcollectionView奇怪的细胞回收行为

ios - 将时间戳设置为 nsuserdefaults

ios - UI 测试 + postNotificationName + 永远不会到达观察者 + Xcode 7

.net - 声明 ClientBase 期间设备崩溃

iphone - 在 iOS 上的 UITabBarController 下方添加 UIViewController 就像 youtube 应用程序

ios - 在 Swift 中为 UITextField 设置光标位置

swift - 如何将电话号码返回到 swift 应用程序?

objective-c - Objective C/Cocoa 中的动态菜单

ios - Xcode 初学者;重用标识符?