swift - 如何在 map 上做手势时添加带有图像的图钉

标签 swift

我想在 map 上添加一个带有手势图像的图钉。 实际上,在手势上添加 pin 功能效果很好,但没有图像,因为它必须是 MKAnnotationView 而不仅仅是 MKAnnotation。 所以我收到了无法修复的奇怪警告:Image of warning

最佳答案

        override func viewDidLoad() {
            super.viewDidLoad()
            let annotation = MKPointAnnotation()
            annotation.title = location.title
            annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
            mapView.addAnnotation(annotation)
    }



func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if annotation is MKUserLocation { return nil }

        let identifier = "CustomAnnotation"

        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)

        if annotationView == nil {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            annotationView!.canShowCallout = false
            annotationView?.backgroundColor = UIColor.clear
            annotationView!.image = UIImage(named: "map-pinpoint.png")!
        } else {
            annotationView!.annotation = annotation
        }
        return annotationView
    }

关于swift - 如何在 map 上做手势时添加带有图像的图钉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51764238/

相关文章:

swift - AVAssetExportSession 给我 AVFoundationErrorDomain Code=-11800

ios - Swift 3 - 设备 token 现在被解析为 '32BYTES'

swift - 映射字典键以添加值 - Swift

ios - 生成错误 Xcode 10 - 生成多个命令

ios - 如何在 swift2 中获取当前的 collectionview 单元格大小?

ios - AVAudioPCMBuffer - 读取(进入缓冲区 : AVAudioPCMBuffer) memory crash?(缓冲区溢出)

ios - 是否可以从我的 pod 的框架资源中的 *.pdf 文件创建 UIImage?

ios - 如何清除 NSMutableAttributedString 中的属性?

ios - 快速捕获无效用户输入的异常

SwiftUI : Using NavigationView leads to image and text gone