Save latitude and longitude in swift

4

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?

    
asked by anonymous 12.09.2016 / 19:30

1 answer

5

Victor,

There are a few different ways to solve your problem. It's not a Swift problem per se, but rather a design . It is not easy to think of the "best way" as soon as the first, the experience comes with time. Come on!

First point: try undocking your code as much as you can. UIViewController should not know how the location is acquired. It should only have one way to get it, with as little knowledge as possible, either by injection dependency or through services. In addition, if you "outsource" localization, the service can be reused by the entire system, and not only by that particular UIViewController .

Second point: do not use global variables. There are a number of reasons to support this claim, which are beyond the scope of this question.

We will try to create a service to obtain location, by pure didactics. Maybe the location issue is not as important to your app and that is well overkill , but this is a public forum and maybe this answer helps other users as well as spreading good practices to the community.

I have created a location-getting service , written in Swift 3. If you want, you can copy your project. If you do this, obtaining the location is totally unbound and in UIViewController (or anywhere you want to get the user's location), the call is reduced to that:

class ViewController: UIViewController {

    @IBAction func qualMinhaLocalizacao() {
        GPSLocationService.shared.obterCoordenada(permitirAnterior: false, callback: self.receber)
    }

    private func receber(coordenada: CLLocationCoordinate2D) {
        NSLog("\(coordenada)")
    }
}

You do not need to pass anything by follows , you do not need a global variable, it's thread-safe and, modesty, elegant part. Please, if anyone finds trouble, tell me what I can do.

I believe that by the way Swift is written, it is possible to understand what happens there. However, if you have any questions, please ask me that I will be happy to help you.

Note: In iOS 9, there is a much simpler way to get single locations, without turning the GPS on and off. Documentation .

Links to read, if you want to study a little about the topics covered!

13.09.2016 / 01:29