I am not able to call MKMapView
delegate method didUpdateUserLocation
.
What I did so far:
Add framework MapKit.framwork
in project
Import framework in view controller by import MapKit
line
Add key in plist file
<key>NSLocationAlwaysUsageDescription</key>
<true/>
Code in view controller
Added delegate didUpdateUserLocation
method to check location is updated or not but never called.
// MARK: - MapView delegate methods
func mapView(mapView: MKMapView!, regionDidChangeAnimated animated: Bool) {
// Calling...
}
func mapView(mapView: MKMapView!, didUpdateUserLocation userLocation: MKUserLocation!) {
// Not getting called
}
// MARK: - View lifeCycle methods
override func viewDidLoad() {
super.viewDidLoad()
var locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
self.mapView.delegate = self
self.mapView.showsUserLocation = true
self.mapView.pitchEnabled = true
self.mapView.showsBuildings = true
}
I have used simulator and change simulator custom location to check whether it is called or not? The method regionDidChangeAnimated
is called perfectly!.
The same thing works for iOS7. what additional effort is remains for Swift map location update?
Edit: Plist keys
Also permission alert not prompted.
CLLocationManager
.locationManager(_:didChangeAuthorizationStatus:)
delegate method called, before .startUpdatingLocation()
and showsUserLocation = true
.NSLocationWhenInUseUsageDescription
value type is String.Try:
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
var locationManager = CLLocationManager()
// MARK: - View lifeCycle methods
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.mapView.delegate = self
self.mapView.pitchEnabled = true
self.mapView.showsBuildings = true
}
// MARK: - LocationManager delegate methods
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
switch status {
case .Authorized, .AuthorizedWhenInUse:
manager.startUpdatingLocation()
self.mapView.showsUserLocation = true
default: break
}
}
// MARK: - MapView delegate methods
func mapView(mapView: MKMapView!, didUpdateUserLocation userLocation: MKUserLocation!) {
println(userLocation)
// Not getting called
}
}