0

I wish to save latitude and longitude coordinates in two labels on a second view controller but I am having trouble getting it to work.

Here is my code:

First View Controller:

import UIKit
import CoreLocation
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

    @IBOutlet var map: MKMapView!

    var locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()

    }

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

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        let userLocation: CLLocation = locations[0]

        let latitude = userLocation.coordinate.latitude

        let longitude = userLocation.coordinate.longitude

        let latDelta: CLLocationDegrees = 0.05

        let lonDelta: CLLocationDegrees = 0.05

        let span = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: lonDelta)

        let location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)

        let region = MKCoordinateRegion(center: location, span: span)

        self.map.setRegion(region, animated: true)

    }    

}

Second View Controller:

import UIKit

class AddSightingViewController: UIViewController {

    var getCoordinates: ViewController!

    @IBOutlet weak var latitudeLabel: UILabel!
    @IBOutlet weak var longitudeLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        latitudeLabel.text = "\(getCoordinates.locationManager.location?.coordinate.latitude)"
        longitudeLabel.text = "\(getCoordinates.locationManager.location?.coordinate.longitude)"


    }

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

}
halfer
  • 19,824
  • 17
  • 99
  • 186
Elfuthark
  • 261
  • 1
  • 4
  • 17
  • Can you give more details about "having trouble getting it to work". What isn't working? What have you tried? Is there code somewhere that sets the value of the `AddSightingViewController.getCoordinates` property? – Kristopher Johnson May 31 '17 at 12:43
  • Sorry @KristopherJohnson my code will either produce coordinates of 0.0 for both lat and long or a finds nil error with the above code – Elfuthark May 31 '17 at 12:47
  • 1
    create a singleton class for getting the location and get the values from there to any view controller – Praveen Kumar May 31 '17 at 12:47
  • 3
    Possible duplicate of [Passing Data between View Controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – DonMag May 31 '17 at 12:52
  • @Futhark - there are many, many, many examples of passing data between view controllers... have a quick search, or start here: https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers?rq=1 – DonMag May 31 '17 at 12:53

2 Answers2

0

when you push VC

objAddSightVC.location = CLLocationCoordinate2D(latitude: your_lat, longitude: your_long)

in AddSightingViewController

var location : CLLocationCoordinate2D!

and

latitudeLabel.text = String(describing: location.latitude)
longitudeLabel.text = String(describing: location.longitude)
dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
0

on storyboard initiate or push VC

let coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long)
let addSightingViewController = AddSightingViewController(location :coordinate)

in AddSightingViewController declare property location with init constructor

class AddSightingViewController: UIViewController {

    let location:LLocationCoordinate2D

    init(location:LLocationCoordinate2D!){

    self.location = location
 }

 override func viewDidLoad() {
        super.viewDidLoad()

        latitudeLabel.text = "\(location?.coordinate.latitude)"
        longitudeLabel.text = "\(location?.coordinate.longitude)"

 }

}
Ashish
  • 2,977
  • 1
  • 14
  • 32