0

I want to get current device location and zoom map to that coordinates.
I also wan't to stop location manager after zoom to user location to prevent changing map visible area if user move.

...viewDidLoad...{
self.locationManager = CLLocationManager();
        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        self.mapView.showsUserLocation = true;
        self.locationManager.startUpdatingLocation();
}

func locationManager(manager:CLLocationManager, didUpdateLocations locations:[AnyObject]){
var newRegion = MKCoordinateRegion(center: self.mapView.userLocation.coordinate, span: MKCoordinateSpanMake(0.014, 0.014));
            self.mapView.setRegion(newRegion, animated: true);
            self.locationManager.stopUpdatingLocation();
}

Problem with this code is that this code move map view to ocean and coordinates are sometimes invalid (another continent).
And as I stopped location manager map view stays in the ocean :-/
What is the proper way to make what I want?

1110
  • 7,829
  • 55
  • 176
  • 334
  • You should always omit self. if needed Xcode will ask you to add it. – Leo Dabus Mar 18 '15 at 23:07
  • here's link you can check > https://stackoverflow.com/questions/22292835/how-to-stop-multiple-times-method-calling-of-didupdatelocations-in-ios/46580983#46580983 –  Oct 05 '17 at 08:25

1 Answers1

0

CLLocationManager doesn't always guarantee the accurate coordinates for single delegate invoke(such as when the first delegation invoke). The newRegion may only give you the cached location.

More about CLLocation, please see these answers:

How to stop multiple times method calling of didUpdateLocations() in ios

CLLocationManager holds cache value

Function "didUpdateToLocation" being called without changes

Community
  • 1
  • 1
Jeff Ma
  • 31
  • 2
  • 9