Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CLLocationManager is slow getting location, Swift

Im creating this app, and it needs to get the users location - its all working properly, the thing is, that the time from accepting the use of location services, to getting the actual location takes like 5 seconds - is this normal? I've used other apps, where it goes much faster..

Here's what my code looks like:

override func viewDidLoad() {
    super.viewDidLoad()

    // Ask for Location-Authorisation from the User.
    self.locationManager.requestWhenInUseAuthorization()

    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        locationManager.requestLocation()
    }

    mapView.delegate = self

}



func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let locValue: CLLocationCoordinate2D = manager.location!.coordinate

    let initialLocation = CLLocation(latitude: locValue.latitude, longitude: locValue.longitude)

    self.centerMapOnLocation(initialLocation)
}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
    print("could not get location")
}

But the time from the application gets the location to put into the centerMapOnLocation-function, seems just to be quite long. What is to be expected, when getting a users location? I'm testing on a wifi connection, so I know its not because the internet is slow, or its a bad connection...

Anyone have an idea? :)

Best regards!

like image 322
Nicolai Harbo Avatar asked Feb 15 '17 09:02

Nicolai Harbo


People also ask

How do I ask a user for location in Swift?

Get A User's Location Once Only once location authorization can be requested by using the CLLocationManager method requestLocation() . Implement the CLLocationManagerDelegate method locationManager(:, didUpdateLocations:) to handle the requested user location.

When didUpdateLocations is called?

didUpdateLocations locations: is called whenever there is update to location / every few seconds until we call locationManager. stopUpdatingLocation. . startUpdatingLocation() might pass more than one location to the didUpdateLocations locations array.


1 Answers

When initializing your location manager, add startUpdatingLocation():

let manager = CLLocationManager()
 manager.delegate = self
 manager.requestWhenInUseAuthorization()
 manager.requestLocation()
 manager.startUpdatingLocation()

Without startUpdatingLocation() geo-location takes about 5 seconds, with it, the request executes nearly immediately.

like image 185
LineDrop Avatar answered Sep 23 '22 17:09

LineDrop