Current Location and Update Location in a MKMapView in swift
Question : I want to do a simple view with a map (MKMapView). I want to find and update the location of the user (like in the Apple Map app).
You have to override
CLLocationManager.didUpdateLocations
(part of CLLocationManagerDelegate) to get notified when the location manager retrieves the current location:func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
let location = locations.last as CLLocation
let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
self.map.setRegion(region, animated: true)
}
NOTE: If your target is iOS 8, you must include the
NSLocationAlwaysUsageDescription
key in your Info.plist to get the location services to work.