【问题标题】:XCode9.4.1 Swift: Showing image instead of text on map annotationsXCode9.4.1 Swift:在地图注释上显示图像而不是文本
【发布时间】:2018-07-19 16:49:14
【问题描述】:

您知道在 MKMapView 中单击注释时标题和副标题是如何显示的。 如何显示图像/图像而不是字幕文本? 我正在使用的自动取款机:

class customPin: NSObject, MKAnnotation {
   var coordinate: CLLocationCoordinate2D
   var title: String?
   var subtitle: String?

   init(pinTitle: String, pinSubTitle: String,    
      Location:CLLocationCoordinate2D) {
      self.title = pinTitle
      self.subtitle = pinSubTitle
      self.coordinate = Location
   }
}

我尝试将“字符串”替换为“UIImage”(在“字幕”处),但这也不起作用

感谢您的帮助, 格斯

【问题讨论】:

    标签: swift dictionary annotations


    【解决方案1】:

    MKAnnotation 只管理引脚的数据,不管理外观。 MKAnnotationView 是引脚的可视化表示。您可以使用mapView(_:viewFor:) 方法操作和生成它:

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        let identifier = "customPin"
        var annotationView: MKAnnotationView
    
        if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView {
            annotationView = dequeuedView
            annotationView.annotation = annotation
        } else {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            annotationView.canShowCallout = true
            annotationView.image = UIImage(named: "example")
        }   
    
        return annotationView
    }
    

    另请参阅:Apple Developer Documentation on MKAnnotationView

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多