0

注释显示在我的地图中,但我无法添加标注。我打算使用标注将 label.text 更改为同一 ViewController 中注释的标题。

例如,我尝试了这个,但我缺少什么让它工作。

我希望有人可以帮助我,因为这个问题我可以用 stackoverflow 或 youtube 解决所有问题,但我现在正在尝试几个小时:(

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate {

    @IBOutlet weak var map: MKMapView!

    override func viewDidLoad() {

        map.delegate = self

        let cor : CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 50, longitude: 10)
        let region = MKCoordinateRegionMakeWithDistance(cor, 5000, 5000)
        self.map.setRegion(region, animated: true)

        let annotation = MKPointAnnotation()
        annotation.coordinate.latitude = 50
        annotation.title = "test"
        annotation.subtitle = "hdhsadsa"
        annotation.coordinate.longitude = 10

        map.addAnnotation(annotation)
    }

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

        if annotation is MKUserLocation { return nil }

        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "identifier") as? MKPinAnnotationView

        if annotationView == nil {
            annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "identifier")
            annotationView?.canShowCallout = true
            annotationView?.rightCalloutAccessoryView = UIButton(type: .infoLight)
        } else {
            annotationView?.annotation = annotation
        }

        return annotationView
    }
}
4

0 回答 0