Установка заголовка MKPointAnnotation при создании при использовании MapKit - PullRequest
0 голосов
/ 18 июня 2019

Итак, на данный момент я не могу понять, как пользователь может ввести заголовок на выводах аннотации. В моем коде булавка появится после долгого нажатия на карте, но название булавки показано как «Булавка».

Единственное, что я хотел бы сделать, - это чтобы пользователь приложения мог создавать уникальные заголовки пинов при создании.

Фонд импорта импорт UIKit импорт MapKit

класс 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)}

}

1 Ответ

0 голосов
/ 18 июня 2019

Пожалуйста, следуйте окончательному коду

@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)}
...