I am learning Swift and doing one project where I want to send an array of CLLocationCoordinate2D from one view controller to another View Controller (Map View) so that I can draw multiple annotation at one time. If my array of CLLocationCoordinate2D has 5 sets of longitudes and latitudes, I want all of them to get passed to my MapViewController and there I want all the annotations to be drawn under in MapView at go without user interactions.
Problem: In my MapView all I see is empty output in console [] but I am passing a set of longitude and latitude from my initial view controller. How can I keep the values intact? `
Problem: Once I get all the coordinates in my Map2ViewController then I can simply loop through all the elements to draw the annotations as the map is loading in the MapView right?
Below is what I have tried until now.
My "SearchBloodViewController.swift" //This is my initial view which generates Longitude and Latitude.
import UIKit
import MapKit
class SearchBloodViewController: UIViewController, UITextFieldDelegate, CLLocationManagerDelegate
{
var array: [PFObject] = [PFObject]()
var arra: [CLLocationCoordinate2D] = [CLLocationCoordinate2D]()
var lat_: Int = 0
var long_: Int = 0
@IBOutlet weak var bloodType: UITextField!
@IBAction func search(sender: UIButton)
{
var query = PFQuery(className: "_User")
query.whereKey("BloodGroup", equalTo: bloodType.text!)
do
{
try array = query.findObjects() as [PFObject]
}
catch
{
}
for arr in array
{
self.lat_ = arr["Latitude"] as! Int
self.long_ = arr["Longitude"] as! Int
var cor = CLLocationCoordinate2D.init(latitude: Double(self.lat_), longitude: Double(self.long_))
arra.append(cor)
}
print(arra)
// The console Output of this is
// [C.CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0),
//C.CLLocationCoordinate2D(latitude: 42.0, longitude: -75.0),
//C.CLLocationCoordinate2D(latitude: 41.0, longitude: -87.0)]
}
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!)
{
if (segue.identifier == "drawmap")
{
let navigationController = segue.destinationViewController as! UINavigationController
let controller = navigationController.topViewController as! Map2ViewController
// controller.delegate = self
controller.ar = self.arra
}
}
override func viewDidLoad()
{
super.viewDidLoad()
}
My Map2ViewController: (Which has MapView)
import UIKit
import MapKit
class Map2ViewController: UIViewController
{
var ar: [CLLocationCoordinate2D] = [CLLocationCoordinate2D]()
@IBOutlet weak var dispAnnotation: MKMapView!
override func viewDidLoad()
{
super.viewDidLoad()
print("The values received are \(ar)")
}
}
Please see: The segue name is "drawmap". The story board has SearchBloodViewController->NavigationController->Map2ViewController