Swift: How can I store coordinates using CLLocationCoordinate2D as a string or an int?

J. Cal picture J. Cal · Mar 21, 2016 · Viewed 11.2k times · Source

So far, this is the code I have. I'm trying to store the values of locValue.latitude and locValue.longitude as either a string or an integer. I've tried defining the return type in the method ( -> String, etc.) but I get an error saying the didChangeAuthorizationStatus method conflicts with optional requirement method locationManager in protocol CLLocationManagerDelegate.

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    if status == .AuthorizedAlways {
        let locValue:CLLocationCoordinate2D = manager.location!.coordinate
        self.dispLocation.text = "locations = \(locValue.latitude) \(locValue.longitude)"
    }
}

Answer

Rashwan L picture Rashwan L · Mar 21, 2016

Just declare two strings

var lat  = ""
var long = ""

And the from your location parse lat and long

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location:CLLocationCoordinate2D = manager.location!.coordinate
    lat = String(location.latitude)
    long = String(location.longitude)
}