我的应用程序中有一个显示用户位置的 map View 。当点击用户的位置时,图像显示如下:
有没有办法根据我的喜好定制这张图片,例如我想显示用户在他自己的个人资料中的照片,或者我想添加的任何其他静态图片。
类似于苹果 map 在您的位置点中显示您的头像的方式。
我怎么能在这里做到这一点?
这是我的代码:
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, 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
关于ios - 如何在 Swift iOS 中更改 MKUserLocation 注释中的默认图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64142912/