4
I have this code in my Viewcontroller to capture the current location of users:
@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 database functions, and I need to save latitude and longitude to store in the database.
How can I store latitude and longitude in a variable outside the locationManager function?
They told me I’d have to turn this: let location : CLLocationCoordinate2D = manager.location!.coordinate
in an instance variable.
Can someone tell me how I solve this problem?
Thank you so much for your help!! I will read these links yes!
– Victor Mendes