Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to speed up getting coordinate location from GPS in Swift?

Tags:

ios

swift

gps

I am a beginner, and I am making an app that get user coordinate. I am making locationManager class like below

import UIKit
import CoreLocation


class LocationManager: NSObject {
    let manager = CLLocationManager()
    var didGetLocation: ((Coordinate?) -> Void)?

    override init() {
        super.init()

        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestLocation()
    }

    func getPermission() {
        // to ask permission to the user by showing an alert (the alert message is available on info.plist)
        if CLLocationManager.authorizationStatus() == .notDetermined {
            manager.requestWhenInUseAuthorization()
        }

    }
}




extension LocationManager : CLLocationManagerDelegate {

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        if status == .authorizedWhenInUse {
            manager.requestLocation()
        }
    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print(error.localizedDescription)
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let location = locations.first else {
            didGetLocation?(nil)
            return

        }
        let coordinate = Coordinate(location: location)
        if let didGetLocation = didGetLocation {
            didGetLocation(coordinate)

        }
    }
}

private extension Coordinate {
    init(location: CLLocation) {
        latitude = location.coordinate.latitude
        longitude = location.coordinate.longitude
    }
}

I just call didGetLocation property to get user Coordinate Location. the code above can get the coordinate data actually. but I think it takes too much time (I got the coordinate after 5 - 7 seconds).

to be honest I am new in iOS development, is that normal to get coordinate location around 5-7 seconds? can I improve this one?

I suspect because the desired accuracy I used is kCLLocationAccuracyBest, but if I change to kCLLocationAccuracyHundredMeters, it seems just the same

so, could I improve this one ? Because If I compare to the android, it just very fast to get coordinate location

like image 818
Alexa289 Avatar asked Mar 06 '18 09:03

Alexa289


1 Answers

As I said in my comment, If you use minor accuracy value lets say kCLLocationAccuracyThreeKilometers you should get a valid location earlier but normally the CLLocationManager take some time to get a valid location because most location manager tasks run asynchronously

What Apple Docs say about this

Declaration

var desiredAccuracy: CLLocationAccuracy { get set }

Discussion

The receiver does its best to achieve the requested accuracy; however, the actual accuracy is not guaranteed.

You should assign a value to this property that is appropriate for your usage scenario. For example, if you need the current location only within a kilometer, you should specify kCLLocationAccuracyKilometer and not kCLLocationAccuracyBestForNavigation. Determining a location with greater accuracy requires more time and more power.

When requesting high-accuracy location data, the initial event delivered by the location service may not have the accuracy you requested. The location service delivers the initial event as quickly as possible. It then continues to determine the location with the accuracy you requested and delivers additional events, as necessary, when that data is available.

For iOS and macOS, the default value of this property is kCLLocationAccuracyBest. For watchOS, the default value is kCLLocationAccuracyHundredMeters.

This property is used only in conjunction with the standard location services and is not used when monitoring significant location changes.

like image 133
Reinier Melian Avatar answered Sep 21 '22 13:09

Reinier Melian