swift - 为什么我的注释标注不会出现?仅当使用自定义图像作为图钉时,标注才有效

标签 swift mapkit mapkitannotation

出于某种原因,当使用自定义图像作为注释时,我的 MapView 上的注释只会显示标注。但是,我喜欢通用注释的干净外观,并且不想使用自定义图像。我的代码有什么问题?

总的来说,我对编程相当陌生,并尝试在不同的网站上提出问题,但找不到答案。

@IBAction func onMoreTapped() {
    print("TOGGLE SIDE MENU")
    NotificationCenter.default.post(name: NSNotification.Name("ToggleSideMenu"), object: nil)
}


@IBAction func mapSwitch(_ sender: UISwitch) {
    if (sender.isOn == true) {
        mapView.mapType = MKMapType.standard
    }
    else {
        mapView.mapType = MKMapType.hybrid
    }

}


var tappedAnnotation : MKAnnotation?

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKPinAnnotationView? {


    if let view = mapView.dequeueReusableAnnotationView(withIdentifier: "Annotation") {
        view.annotation = annotation
        return view as! MKPinAnnotationView
    } else {
        let view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "Annotation")
        view.isEnabled = true
        view.canShowCallout = true
        view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
        return view
    }
}

func mapView(_ mapView: MKMapView, annotationView view: MKPinAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    tappedAnnotation = view.annotation
    performSegue(withIdentifier: "showAnnotationDetails", sender: nil)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showAnnotationDetails", let dest = segue.destination as? AnnotationDetails {
        dest.annotation = tappedAnnotation
    }
}



func createAnnotations ( _ annotations : [String:[String:Any]] ) {
    mapView.removeAnnotations(mapView.annotations)
    for (_,values) in annotations {
        if let latDouble = values["latitude"] as? Double, let longDouble = values["longitude"] as? Double {
            let lat = CLLocationDegrees( latDouble )
            let long = CLLocationDegrees( longDouble )
            let coord = CLLocationCoordinate2D(latitude: lat, longitude: long)
            let annotation = MKPointAnnotation()
            annotation.coordinate = coord
            annotation.title = values["name"] as? String ?? ""
            annotation.subtitle = values["info"] as? String ?? ""
            mapView.addAnnotation(annotation)
            courtAmountLabel.title = "\(mapView.annotations.count-1) Courts"
        }
    }
}

let manager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()


    courtAmountLabel.tintColor = UIColor.black


    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.requestWhenInUseAuthorization()
    manager.startUpdatingLocation()

    mapView.delegate = self


    //
    mapView.showsUserLocation = true

    mapView.mapType = MKMapType.standard


    let ref = Database.database().reference()


    ref.child("annotations").observe(.value) { snapshot in 
        print(snapshot)
        if let annotations = snapshot.value as? [String:[String:Any]] {
            self.createAnnotations(annotations)
        } else {
            print("Data received not formatted as expected")
        }
    }

我希望出现注释标注,这将允许应用用户按下并访问另一个页面。

最佳答案

使用

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

而不是

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKPinAnnotationView? {

关于swift - 为什么我的注释标注不会出现?仅当使用自定义图像作为图钉时,标注才有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57582496/

相关文章:

mapkit - MKMarkerAnnotationView 簇颜色

Swift 撤消功能,先前声明的变量始终为空

Swift 初始化存储属性在类问题的 init 方法之外

ios - 如何正确地使 UICollectionView 中补充 View 的布局无效

json - 将 MKPolygon 存储在 Realm 数据库中

ios - 使用 MKMapView 和 MKRoute 获取方向问题

ios - MKMapView:偏移贴图使标注可见

ios - 当父 View 改变大小时,如何告诉 subview 调整大小?

swift - map 套件图钉下的字符串(mkannotationview 图像)

ios - 如何更改用户位置注释的接近度