0

So, at the moment I can't seem to figure out how the user can enter a title on the Annotation pins. In my code a pin will appear after a long press on the map, but the title shown of the pin is "Pin".

The only thing I would like to be able to do is for the user of the app to be able to create unique titles of the pins on creation.

import Foundation import UIKit import 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)}

}

JedetHe
  • 3
  • 5

1 Answers1

0

Please follow the final code

@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)}
komara
  • 334
  • 1
  • 7
  • Thank you so much for having tolerance with me! With the newly added code There doesnt seem to show up any pins at all. However upon pressing the screen and changing the popup shows and i fill the textfield, but no pin is created. – JedetHe Jun 18 '19 at 07:17
  • //replace self.addPinWithTitle (sender , title : textField?.text ?? "") //with DispatchQueue.main.async { self.addPinWithTitle (sender , title : textField?.text ?? "") } – komara Jun 18 '19 at 07:41
  • But I don't think so the pin will place on the exact location. – komara Jun 18 '19 at 07:49
  • Hmm even with the newest addition the pin doesnt seem to show up on the map :S i updated the code – JedetHe Jun 18 '19 at 07:56
  • I created new project for u which works fine for me. download it from below link : - https://drive.google.com/open?id=13qDFNtpsPdR-dyN2jf4PfFlZVNI7vYcK – komara Jun 18 '19 at 08:05
  • Yeah it works! but as you said the pins show up way higher at the map . haha, no idea why – JedetHe Jun 18 '19 at 08:15