I have this code in my ViewController to capture the users current location:
@IBOutlet weak var userMapView: MKMapView!
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = manager.location!.coordinate
print(location.latitude)
let latitudeDelta: CLLocationDegrees = 0.01
let longitudeDelta: CLLocationDegrees = 0.01
let span = MKCoordinateSpanMake(latitudeDelta, longitudeDelta)
let center = CLLocationCoordinate2DMake(location.latitude, location.longitude)
let region = MKCoordinateRegionMake(center, span)
self.userMapView.setRegion(region, animated: false)
}
I have another swift file with the database functions, and I need to save the latitude and longitude to store in the database.
How can I store latitude and longitude in a variable outside of the locationManager function?
I was told that I would have to turn this: let location : CLLocationCoordinate2D = manager.location!.coordinate
into an instance variable.
Does anyone know how to solve this problem?