0

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

3
  • I think you don't need this line var ar: [CLLocationCoordinate2D] = [CLLocationCoordinate2D]() becuase you have to print only var ar don't initialize again. Commented Nov 24, 2015 at 11:12
  • And you can also create the property of an array for the same and print that array so that you get answer. Commented Nov 24, 2015 at 11:26
  • @BhadreshMulsaniya : Got my bug fixed. Just see the answer I have given. I don't know if its the right approach but I am good for now. :) Commented Nov 24, 2015 at 14:38

3 Answers 3

1

On Map2ViewController, in this line:

var ar: [CLLocationCoordinate2D] = [CLLocationCoordinate2D]()

You're creating an array with a default value (a new instance of an array with data type CLLocationCoordinate2D), so the content of ar is overriten by an empty array when you are loading this controller, you just need to create a nullable var, that can be assigen through the segue:

var ar : [CLLocationCoordinate2D]?

Everything else looks fine.

EDIT: ar is an optional var, so if you want to print it's content you need to unwrap it like this:

if let notNullArray = ar {
  print("The values received are \(notNullArray)")
}
Sign up to request clarification or add additional context in comments.

14 Comments

It does not seem to be working: The new console output I get i "The values received are Optional([]) "
I've updated the answer, you should read and learn about optional values on swift, this answer explains them really well, stackoverflow.com/questions/24003642/…
Hello Fantini, I have kind of gone through that. But I guess there is some other bug. Even after trying the second method that you have suggested I am not getting the output. Thank you for the effort. Please let me know if you find any other mistake.
the new output is "The values received are []"
During debugging I noticed that my search Action is getting called long after the segue has executed this statement controller.ar = self.arra. The search method is hit later so I want my search action to be called before the segue executes this line "controller.ar = self.arra"
|
0
let controller = navigationController.topViewController as! Map2ViewController

When above line is called your viewDidLoad will be called then only as your navigation controller will present Map2ViewController. Therefore it will print a blank array in your viewDidLoad method of Map2ViewController. I recommend you to pass the array in initializer method of Map2ViewController.

The above method is for using without storyboards. For your case I would recommend you to do whatever you want in setter method of ar variable of your Map2ViewController.

You can do it as follows:

var ar: [CLLocationCoordinate2D] = {
     didSet {
       print("The values received are \(ar)")
     }
}

1 Comment

Can you please elaborate. Thanks.
0

Got my bug fixed. Actually I am setting all the values of coordinates in my Search Action. What I am doing now is I took entire logic that was there in Search action and put it in Prepareforsegue section. So now my Search action is empty and just doing one thing which is letting me go to the next screen which is map2ViewController. where as the whole computation and setting of longitude and latitude code has been put in prepare for segue method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.