Jason Chan
Jason Chan

Reputation: 27

how to pass longitude and latitude from CLLocationManager to use globally swift

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        var locValue:CLLocationCoordinate2D = manager.location.coordinate
        println("locations = \(locValue.latitude) \(locValue.longitude)")


    CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: {(placemarks, error)->Void in

        if (error != nil) {
            println("Error: " + error.localizedDescription)
            return
        }

        if placemarks.count > 0 {
            let pm = placemarks[0] as CLPlacemark
            self.locationManager.stopUpdatingLocation()
            //self.dismissViewControllerAnimated(true, completion: nil)
            self.displayLocationInfo(pm)


        } else {
            println("Error with the data.")
        }


    })

}

Currently I'm able to get the current location with coordinate and I'm able to print it. But how can I send the value in the function to web service? I found that the variable is not accessible outside of the function. Can anyone help? Thanks a lot!

Upvotes: 1

Views: 1038

Answers (1)

Vasil Garov
Vasil Garov

Reputation: 4921

If you want to access your location variable outside the CLLocationManagerDelegate method you should declare it right where your class definition starts:

class MyClass {
    private var currentCoordinate: CLLocationCoordinate2D?
}

Then in your location delegate method you assign the current value:

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    currentCoordinate = manager.location.coordinate
}  

Upvotes: 1

Related Questions