【问题标题】:how to change default image in MKUserLocation annotation in Swift iOS如何在 Swift iOS 中更改 MKUserLocation 注释中的默认图像
【发布时间】:2020-09-30 17:35:38
【问题描述】:

我的应用中有一个显示用户位置的地图视图。当用户的位置被点击时,图像显示如下:

有没有办法根据我的喜好定制此图像,例如,我想显示用户在他自己的个人资料中拥有的照片,或者我想添加的任何其他静态图像。

类似于苹果地图在您的位置点中显示您的个人资料图片的方式。

我该怎么做?

这是我的代码:

 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
       if annotation is MKUserLocation {
        print("user location image tapped")
        
           return nil
       }

       let reuseId = "pin"
       var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
       if pinView == nil {
           pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
           pinView?.animatesDrop = true
           pinView?.canShowCallout = true
           pinView?.isDraggable = true
        
         //  pinView?.pinColor = .purple

           let rightButton: AnyObject! = UIButton(type: UIButton.ButtonType.detailDisclosure)
           pinView?.rightCalloutAccessoryView = rightButton as? UIView
   
       }
       else {
           pinView?.annotation = annotation
       }
       
       return pinView
   }

对此有什么帮助吗?谢谢。

【问题讨论】:

  • 您是自己写的还是抄袭的?不要误会我的意思,但有一些我不知道的零控制,你能解释更多细节吗?你为什么使用 MKPinAnnotation 而不是 MKAnnotationView ?
  • 因为使用 MKPinAnnotation 我可以弹出窗口,向用户查看 MKAnnotationView 不支持的更多详细信息。但这不是问题所在,您对我的问题有什么不明白的地方?@sekoyaz
  • hmm 然后试试这个,我补充回答会很有帮助@mike

标签: ios swift mkmapview mkannotation mkannotationview


【解决方案1】:
let imageView = UIImageView(frame: CGRect(0, 0, 25, 25))
imageView.image = UIImage(named: "userpic");
imageView.layer.cornerRadius = imageView.layer.frame.size.width / 2
imageView.layer.masksToBounds = true
anotationView.frame = imageView.frame

【讨论】:

  • 谢谢你的回答,但是当我尝试这个时它告诉我注解没有成员框架
  • 注释视图有框架我的朋友,默认为 0 :l
  • 我将把上面的代码放在我的代码段中的什么地方?因为它向我显示错误并说它没有框架。你是在一般情况下编码还是在我的 mapView 函数范围内编码?
  • 是的,这就是问题所在,我应该把上面的代码放在哪里?我有几个 mapview 函数,我会把这段代码放在哪里来实现我需要的?谢谢
  • 我是这样做的,请检查我编辑的问题
猜你喜欢
  • 1970-01-01
  • 2014-11-27
  • 2014-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多