【问题标题】:How can I use the generic Apple Maps pins for locations in MapKit, rather than having to use my own images as pins?如何在 MapKit 中使用通用 Apple 地图图钉作为位置,而不必使用我自己的图像作为图钉?
【发布时间】:2019-08-18 02:23:02
【问题描述】:

在我的应用程序中,我已将坐标存储在 FireBase 中,并在下面的代码中的地图视图上显示这些坐标。用户的当前位置也被利用。我希望我存储在 FireBase 中的位置由通用 Apple Maps 引脚表示,但我不知道如何让应用程序工作,除非我使用自定义图像作为引脚。此外,地图上显示的代表当前位置的用户通用“蓝点”不会出现在我面前。在位置存储在应用程序中而不是在 FireBase 中的不同项目中,通用图钉和蓝点看起来就像我想要的那样。我该如何解决这个问题?

我尝试过更改代码的零碎部分,但我对 Swift 和整个编程世界都比较陌生,找不到解决方案。

类 ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet weak var mapView: MKMapView!


@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) -> MKAnnotationView? {


    if let view = mapView.dequeueReusableAnnotationView(withIdentifier: "Annotation") {
        view.annotation = annotation
         view.image = UIImage(named: "pin")
        return view
    } else {
        let view = MKAnnotationView(annotation: annotation, reuseIdentifier: "Annotation")
        view.image = UIImage(named: "pin")
        view.isEnabled = true
        view.canShowCallout = true
        view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
        return view
    }
}

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, 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)
        }
    }
}

let manager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()

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

    mapView.delegate = self

    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")
        }
    }
}

【问题讨论】:

    标签: swift firebase-realtime-database mapkit mapkitannotation


    【解决方案1】:
    • 使用MKPinAnnotationView 代替MKAnnotationView
    • 设置mapView.showsUserLocation = true 显示用户位置(蓝点)

    【讨论】:

    • 谢谢!引脚和位置完全符合我的要求。但是,当我点击注释时,以前在自定义引脚上的标注按钮现在消失了。这是什么原因?再次感谢。
    • 你删除了view.canShowCallout = true view.rightCalloutAccessoryView 吗?
    • 我没有删除它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-04
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    相关资源
    最近更新 更多