Save latitude and longitude in Swift

Asked

Viewed 287 times

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?

1 answer

5


Victor,

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

First point: try to uncouple your code as much as possible. O UIViewController must not know how the location is acquired. He must only have a way of obtaining it, with as little knowledge as possible, whether by injection of dependency or through services. In addition, if you "outsource" obtaining the location, the service can be reused by the entire system, and not only by this UIViewController specifically.

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

We will try to create a service to obtain localization, by pure didactic. Maybe the issue of location isn’t so important to your app and that’s fine Overkill, but this is a public forum and maybe this answer helps other users, in addition to spreading good practices to the community.

I created for you here service to obtain locations, written in Swift 3. If you want, you can copy to your project. If you do this, getting the location is totally uncoupled and on UIViewController (or anywhere you want to get the user’s location), the call is reduced to this:

class ViewController: UIViewController {

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

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

You don’t have to go through anything follow, does not need a global variable, it is thread-safe and, modesty part, elegant. Please, if anyone finds trouble, tell me that I arrange.

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

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

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

  • Thank you so much for your help!! I will read these links yes!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.