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

2

5

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)"
    }
}
Frey answered 21/3, 2016 at 22:15 Comment(2)
store them where? Why not just store them as the original format?Asthma
My intention is to store the values as variables so I can put them in an API url to accommodate changing locationFrey
T
8

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)
}
Trude answered 21/3, 2016 at 22:25 Comment(1)
Sorry, this is my first time posting on StackOverflow. Thanks for the advice.Frey
R
3

you can also try this -

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
    var locValue:CLLocationCoordinate2D = CLLocationCoordinate2DMake(0, 0);

    if manager.location?.coordinate != nil {
        locValue = (manager.location?.coordinate)!
    }

    let locations = "locations = \(locValue.latitude) \(locValue.longitude)"
    print(locations)
}
Ravi answered 21/3, 2016 at 22:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.