单击 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 (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...