使用 MapKit 时在创建时设置 MKPointAnnotation 的标题

Setting the title of a MKPointAnnotation on creation when using MapKit

所以,目前我似乎无法弄清楚用户如何在注释图钉上输入标题。在我的代码中,在地图上长按后会出现一个图钉,但图钉显示的标题是 "Pin".

我唯一想做的就是让应用程序的用户能够在创建时为图钉创建唯一的标题。

导入基金会 导入 UIKit 导入 MapKit

class MapsViewController: UIViewController, CLLocationManagerDelegate {

let locationManager:CLLocationManager = CLLocationManager()

@IBOutlet weak var mapView: MKMapView!

override func viewDidLoad() {
    super.viewDidLoad()


    locationManager.delegate = self
    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    for currentLocation in locations{
        print("\(index) : \(currentLocation)")
    }
}

@IBAction func addPin(_ sender: UILongPressGestureRecognizer) {

    let alert = UIAlertController(title: "Title", message: "Please enter location name", preferredStyle: .alert)

    //2. Add the text field. You can configure it however you need.
    alert.addTextField { (textField) in
        textField.text = "Location name"
    }
    // 3. Grab the value from the text field, and print it when the user clicks OK.
    alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { [weak alert] (_) in
        let textField = alert?.textFields![0] // Force unwrapping because we know it exists.
        print("Text field: \(String(describing: textField?.text))")
        DispatchQueue.main.async { self.addPinWithTitle (sender , title : textField?.text ?? "") }
    }))
    self.present(alert, animated: true, completion: nil)}

func addPinWithTitle(_ sender: UILongPressGestureRecognizer , title : String) {
    let location = sender.location(in: self.mapView)
    let locCoordinates = self.mapView.convert(location, toCoordinateFrom: self.mapView)

    let annotation = MKPointAnnotation()

    annotation.coordinate = locCoordinates
    annotation.title = title

    self.mapView.addAnnotation(annotation)}

}

请按照最终代码

@IBAction func addPin(_ sender: UILongPressGestureRecognizer) {

let alert = UIAlertController(title: "Title", message: "Please enter location name", preferredStyle: .alert)

//2. Add the text field. You can configure it however you need.
alert.addTextField { (textField) in
    textField.placeholder = "Location name"
}

// 3. Grab the value from the text field, and print it when the user clicks OK.
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { [weak alert] (_) in
    let textField = alert?.textFields![0] // Force unwrapping because we know it exists.
    print("Text field: \(textField?.text)")
    DispatchQueue.main.async {
            self.addPinWithTitle (sender , title : textField?.text ?? "")
        }
}))
self.present(alert, animated: true, completion: nil)}

func addPinWithTitle(_ sender: UILongPressGestureRecognizer , title : String) {
let location = sender.location(in: self.mapView)
let locCoordinates = self.mapView.convert(location, toCoordinateFrom: self.mapView)

let annotation = MKPointAnnotation()

annotation.coordinate = locCoordinates
annotation.title = title

self.mapView.addAnnotation(annotation)}