Использование массива для передачи координат, которые будут размещены в MkMapView

У меня есть один контроллер вида, который позволяет пользователю вводить координаты в текстовые поля и обновляет эти координаты (а также заголовок) в массив. В моем MapView VC я хочу иметь возможность размещать булавки в соответствии с тем, что сделал пользователь. Однако, когда я запускаю код, входные координаты не читаются и ничто не помещается в MapView. У меня есть предположение, что я не сохраняю эти местоположения должным образом, или мое расположение булавок не работает, но я не уверен, где может быть ошибка.

Входные координаты:

import UIKit
import CoreLocation

// here I initialize my array locations
var locations: [Dictionary<String, Any>] = []

class OtherVC: UIViewController {

@IBOutlet weak var latitudeField: UITextField!
@IBOutlet weak var longitudeField: UITextField!

@IBOutlet weak var titleTextField: UITextField!
var coordinates = [CLLocationCoordinate2D]()

override func viewDidLoad() {
    super.viewDidLoad()
}

// this IBOutlet takes the input from the textfield
@IBAction func addToMap(_ sender: Any) {
    let lat = latitudeField.text!
    let long = longitudeField.text!
    let title = titleTextField.text!
    let location: [String: Any] = ["title": title, "latitude": lat, "longitude": long]
    locations.append(location)

}

Ниже приведен код для моего MapView:

import UIKit
import MapKit
class MapViewController: UIViewController, MKMapViewDelegate {

@IBOutlet weak var mapView: MKMapView!

override func viewDidLoad() {
    super.viewDidLoad()
    mapView.delegate = self


    //iterates each location in my array to pull the title and coordinate
    for location in locations {
        let annotation = MKPointAnnotation()
        annotation.title = location["title"] as? String
        annotation.coordinate = CLLocationCoordinate2D(latitude: location["latitude"] as! Double, longitude: location["longitude"] as! Double)
        mapView.addAnnotation(annotation)
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let identifier = "pinAnnotation"
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView

    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        annotationView?.canShowCallout = true
    }

    annotationView?.annotation = annotation
    return annotationView
}
}


}

0 ответов

Другие вопросы по тегам