0

I'm building an iOS app using MapKit, and CoreLocation. I can get the current location of the user by calling :

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.setupUI()

    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.mapView.delegate = self
    self.locationManager.startUpdatingLocation()
    self.mapView.showsUserLocation = true
    geoCoder = CLGeocoder()
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let location: CLLocation = locations.last!
        //        self.addressView.text = "\(location.coordinate.latitude) \(location.coordinate.longitude)"

        geoCode(location)
        if (zoomed == false) {
            zoomToUserLocationAnimated(true)
            zoomed = true
        }
    }

But this function is called even when the user does not move. I'm using iOS simulator on OSX and I simulated a location, so I'm pretty sure the simulator does not move, but still this function is called, I'd like to know if there is a way to check if the user made an actual move ?

Thanks

Nicolas Charvoz
  • 1,509
  • 15
  • 41
  • How are you starting the Location Manager Service? – Abizern Apr 11 '16 at 12:50
  • Try using [startMonitoringSignificantLocationChanges()](https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/#//apple_ref/occ/instm/CLLocationManager/startMonitoringSignificantLocationChanges) instead of `startUpdatingLocation()` – Abizern Apr 11 '16 at 12:58

1 Answers1

0

You will have to compare the previous location with the current location. However note that the location is never absolutely precise so you should make sure to compare with some error.

e.g. location.distanceTo(prevLocation) > 3.

Also see CLLocation.horizontalAccuracy and CLLocation.verticalAccuracy.

Note that the location is often calculated from GPS and the GPS satellites also move.

Another option is to check the speed location.speed > X where X is some nonzero value (again, it's not very safe to check against zero).

Sulthan
  • 128,090
  • 22
  • 218
  • 270