0
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {


        let userLocation: CLLocation = locations[0]

        let center = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)

        mylat = userLocation.coordinate.latitude
        mylong = userLocation.coordinate.longitude

        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.04, longitudeDelta: 0.04))

        totalMap.setRegion(region, animated: true)
}

first.. i'm sorry for my english writning...

i used to mylat , mylong in that func .. but i want to add user recently location with global variable and i use that user location in viewDidLoad..

actually i need user location(lat,long) and many other location, and then i use comparing opration with two location, and i need to most of nearly location ..

help me

iBug
  • 2,334
  • 3
  • 32
  • 65
  • Id like to refer you to my answer in similar thread were I made a through explanation of how it works: http://stackoverflow.com/questions/25296691/swift-get-users-current-location-coordinates/38568439#38568439 – MiladiuM Nov 10 '16 at 07:46
  • 1
    i will follow you ^^ – 김시원 Nov 21 '16 at 05:29

1 Answers1

0

The CLLocationManager works asynchronously. You can use in your viewDidLoad() but not as you might think.

You can register a callback like this:

var onLocationUpdate: ((CLLocation) -> ())?
override func viewDidLoad() {
    super.viewDidLoad()
    onLocationUpdate = { [weak self] location in
        // do something with the location
    }
}

and call the onLocationUpdate?(YOUR_LOCATION) in the locationManager(_:didUpdateLocations:) method

Luca D'Alberti
  • 4,749
  • 3
  • 25
  • 45