I need to get the current location of the user, but currentLocation return alawas nil. However, CLLocationManager.authorizationStatus is authorizedWhenInUse and on the Map, the pin of my location si correct.
import Foundation
import CoreLocation
class Location: NSObject, CLLocationManagerDelegate {
var currentLocation: CLLocation? = nil
var showUserLocation: Bool?
var isAuthorised = false {
didSet {
if isAuthorised {
locationManager.startUpdatingLocation()
showUserLocation = true
} else {
locationManager.stopUpdatingLocation()
currentLocation = nil
showUserLocation = false
}
}
}
private let locationManager = CLLocationManager()
override init() {
super.init()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
currentLocation = locations.last
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
isAuthorised = (status == .authorizedWhenInUse)
}
func distance(of position: Position) -> CLLocationDistance {
let location = CLLocation(latitude: position.lat, longitude: position.lng)
return currentLocation?.distance(from: location) ?? Double.infinity
}
}
extension CLLocationCoordinate2D {
var position:Position {
return Position(lat: latitude, lng: longitude)
}
}
Thank you !
EDIT : I HAVE FIND THE SOLUTION !!!
In my MapViewController, I loaded the annotation before init the Map. So, the function distance was execute BEFORE the function didUpdateLocations.
I am very apologize for the lost time... Thank you very much for your help.
I have written the full code to fetch the current location through the LocationManager
class. This code is within the LocationManager.swift
file.
import Foundation
import CoreLocation
typealias LocationHandler = (_ location: CLLocation?, _ error: Error?) -> Void
class LocationManager: NSObject {
var locationManager: CLLocationManager?
var locationHandler: LocationHandler?
override init() {
super.init()
setupLocation()
}
fileprivate func setupLocation() {
locationManager = CLLocationManager()
locationManager?.desiredAccuracy = kCLLocationAccuracyBestForNavigation
locationManager?.distanceFilter = kCLLocationAccuracyBest
locationManager?.delegate = self
locationManager?.requestWhenInUseAuthorization()
}
public func findCurrentLocation(_ handler: LocationHandler?) {
if isLocationPermissionEnabled() {
locationHandler = handler
locationManager?.startUpdatingLocation()
}
}
}
extension LocationManager: CLLocationManagerDelegate {
public func isLocationPermissionEnabled() -> Bool {
let status = CLLocationManager.authorizationStatus()
return (status == .denied || status == .notDetermined) ? false : true
}
func locationManager(_: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let currentLocation = locations.last {
self.locationHandler?(currentLocation, nil)
}
}
func locationManager(_: CLLocationManager, didFailWithError error: Error) {
self.locationHandler?(nil, error)
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.