Swift MKMapView KVO
Swift MKMapView KVO
我正在尝试为我的用户当前位置创建一个键值观察,但我无法获得 KVO 的方法来触发任何信息。已设置 mapView 的委托。
//add observer here
self.mapView.addObserver(self, forKeyPath: "userLocation", options: NSKeyValueObservingOptions.new, context: nil)
//my method
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
print(keyPath!)
}
如果你想知道你当前位置什么时候改变,你可以试试CLLocationManager class 并使用他的委托方法
func locationManager(_ manager: CLLocationManager,
didUpdateLocations locations: [CLLocation])
希望对您有所帮助:)
使用CLLocationManager
:
class ViewController: UIViewController, CLLocationManagerDelegate {
// You must hold a strong reference to the location manager so make it an instance property
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
// Check your app's authorization status
switch CLLocationManager.authorizationStatus() {
case .authorizedWhenInUse, .authorizedAlways:
// Authorized, start tracking location
self.startUpdatingLocation()
case .notDetermined:
// Not authorized, ask for permission. You can also replace this with
// locationManager.requestAlwaysAuthorization() if your app needs it
locationManager.requestWhenInUseAuthorization()
default:
print("permission denied")
break;
}
}
// MARK: -
func startUpdatingLocation() {
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse || status == .authorizedAlways {
self.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let lastLocation = locations.last else {
return
}
// now do what you want with the user's location
}
}
请记住将 NSLocationWhenInUseUsageDescription
或 NSLocationAlwaysUsageDescription
添加到您的 Info.plist
中,以解释为什么您需要知道用户的位置。还要为用户未授予您访问 phone 位置的权限的情况做好准备 - 您必须决定应用的哪些部分可以 运行 知道用户的位置。
我正在尝试为我的用户当前位置创建一个键值观察,但我无法获得 KVO 的方法来触发任何信息。已设置 mapView 的委托。
//add observer here
self.mapView.addObserver(self, forKeyPath: "userLocation", options: NSKeyValueObservingOptions.new, context: nil)
//my method
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
print(keyPath!)
}
如果你想知道你当前位置什么时候改变,你可以试试CLLocationManager class 并使用他的委托方法
func locationManager(_ manager: CLLocationManager,
didUpdateLocations locations: [CLLocation])
希望对您有所帮助:)
使用CLLocationManager
:
class ViewController: UIViewController, CLLocationManagerDelegate {
// You must hold a strong reference to the location manager so make it an instance property
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
// Check your app's authorization status
switch CLLocationManager.authorizationStatus() {
case .authorizedWhenInUse, .authorizedAlways:
// Authorized, start tracking location
self.startUpdatingLocation()
case .notDetermined:
// Not authorized, ask for permission. You can also replace this with
// locationManager.requestAlwaysAuthorization() if your app needs it
locationManager.requestWhenInUseAuthorization()
default:
print("permission denied")
break;
}
}
// MARK: -
func startUpdatingLocation() {
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse || status == .authorizedAlways {
self.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let lastLocation = locations.last else {
return
}
// now do what you want with the user's location
}
}
请记住将 NSLocationWhenInUseUsageDescription
或 NSLocationAlwaysUsageDescription
添加到您的 Info.plist
中,以解释为什么您需要知道用户的位置。还要为用户未授予您访问 phone 位置的权限的情况做好准备 - 您必须决定应用的哪些部分可以 运行 知道用户的位置。