微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在Swift iOS中更改MKUserLocation批注中的默认图像

如何解决如何在Swift iOS中更改MKUserLocation批注中的默认图像

我的应用程序中有一个地图视图,用于显示用户的位置。点击用户的位置后,图像将显示如下:

enter image description here

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

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

我该怎么办?

这是我的代码

 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
   }

对此有任何帮助吗?谢谢。

解决方法

let imageView = UIImageView(frame: CGRect(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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。