如何解决单击 Mapkit Annotation 时不显示视图Swift,iOS 14
我的应用程序中有一个地图视图,它应该显示用户所有具有位置的照片的图钉。目前,在测试下面的代码时,我可以在地图上看到图钉,但单击时标注不会出现在图钉上方。我已经尝试了许多关于这个主题的类似问题,但到目前为止都没有成功。这些是我正在使用的代码片段:
import Foundation
import MapKit
class MapViewController: UIViewController,MKMapViewDelegate{
var user: User?
@IBOutlet var mapView: MKMapView!
static var reuseIdentifier = "Annotation"
//view loaded,pull all location data from photos in DB
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
mapView.register(MKAnnotationView.self,forAnnotationViewWithReuseIdentifier: MapViewController.reuseIdentifier)
addPhotoAnnotations()
}
/** Adds Annotations to the map view for each photo that has a location */
private func addPhotoAnnotations(){
if user != nil {
for id in user!.photosMap { //for each photo
let tempPhoto = user!.getPhoto(id: id)
if tempPhoto!.location != nil { //if the photo has a location
let tempAnnotation = MKPointAnnotation()
tempAnnotation.coordinate = tempPhoto!.location!.coordinate
mapView.addAnnotation(tempAnnotation)
}
}
}else{
print("User object passed to map VC was null")
}
}
如您所见,我确保设置了 mapView 的委托,并使用预设标识符注册了注释。下面是我的 viewFor 和 calloutAccessoryControlTapped 函数。
func mapView(_ mapView: MKMapView,viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard annotation is MKPointAnnotation else { return nil }
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: MapViewController.reuseIdentifier) as? MKPinAnnotationView
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation,reuseIdentifier: MapViewController.reuseIdentifier)
annotationView!.canShowCallout = true
annotationView!.animatesDrop = true
} else {
annotationView!.annotation = annotation
}
let button = UIButton(type: UIButton.ButtonType.custom) as UIButton
button.setimage(UIImage(named: "photo.fill"),for: .normal)
annotationView?.rightCalloutAccessoryView = button
annotationView?.prepareFordisplay()
return annotationView
}
//this function is called when the photo button on the annotation view is clicked
func mapView(_ mapView: MKMapView,annotationView view: MKAnnotationView,calloutAccessoryControlTapped control: UIControl) {
if control == view.rightCalloutAccessoryView{
//Take the user to the singlePhotoView for that photo
print("got here")
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。