0

Im trying to create a Key Value Observation for my user current location but I can not get the method of KVO to trigger any information. the delegate for mapView is set.

//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!)
    }
rmaddy
  • 314,917
  • 42
  • 532
  • 579
SwiftER
  • 1,235
  • 4
  • 17
  • 40
  • See this answer: http://stackoverflow.com/questions/2473706/how-do-i-zoom-an-mkmapview-to-the-users-current-location-without-cllocationmanage/2578459#2578459 `self.mapView.userLocation.location` is the property that contains the current location coordinates. You need to observe that property for changes. – Darren Feb 15 '17 at 01:44

2 Answers2

0

if you want to know when your current location changes, you can try the CLLocationManager class and use his delegate method

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

I hope it helps :)

Luciano Almeida
  • 241
  • 1
  • 6
0

Use 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
    }
}

Remember to add NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription to your Info.plist to explain why you need to know the user's location. Also prepare for the case when the user does not give you permission to access the phone's location - you have to decide what part of your app can run with knowing the user's location.

Code Different
  • 90,614
  • 16
  • 144
  • 163