0

I am using location.requestLocation in swift 5 for my ios app. But its taking way too long time. around 10 seconds. searching for solutions online, they are saying use startUpdatingLocation instead..but problem is I only want it once... not continuous update. I am also not interested in it being accurate.I can have it wrong by some margin. Because all I want is just zoom to my google map position, and sort stuff by distance using user location. Location can be wrong up to 3 - 4 Kilometers I have no problem.

 override func viewDidLoad() {
        super.viewDidLoad()
        //mapView.showsUserLocation = false
        locationManager.delegate = self
        print(locationManager.distanceFilter)
        switch CLLocationManager.authorizationStatus() {
        case CLAuthorizationStatus.notDetermined, .restricted, .denied:
            locationManager.requestWhenInUseAuthorization()
        case CLAuthorizationStatus.authorizedWhenInUse, .authorizedAlways:
            requestLocation()
        @unknown default: break

        }            
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        print("\nStart of locationManager(didChangeAuthorization)")

        let authStatus = CLLocationManager.authorizationStatus()
        if authStatus == CLAuthorizationStatus.authorizedWhenInUse
            || authStatus == CLAuthorizationStatus.authorizedAlways {
            requestLocation()
        }

        print("\nEnd of locationManager(didChangeAuthorization)")
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        print("\nStart of locationManager(didUpdateLocations)")

        zoomInLocation(locations.last!)
    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        if let err = error as? CLError, err.code == .denied {
            manager.stopUpdatingLocation()
            return
        }
        print("\nlocationManager(): \(error.localizedDescription)")
    }

    private func requestLocation() {
        print("\requestLocation() called")

        // check if the location service is availalbe on that device
        if !CLLocationManager.locationServicesEnabled() {
            return
        }

        locationManager.requestLocation()
    }

    private func zoomInLocation(_ location: CLLocation) {
        loca2  = location
        let camera = GMSCameraPosition.camera(withLatitude: location.coordinate.latitude, longitude: location.coordinate.longitude, zoom: 6.0)
        (view1 as! GMSMapView).animate(to: camera)
        print("\nzoomInUserLocation(): mapView[latitude]=\(location.coordinate.latitude), locationManager[latitude]=\(String(describing: location.coordinate.latitude))")
        let coordinateSpan = MKCoordinateSpan(latitudeDelta: 20, longitudeDelta: 20)
        _ = MKCoordinateRegion(center: location.coordinate, span: coordinateSpan)
        // mapView.centerCoordinate = location.coordinate
        //mapView.setRegion(coordinateRegion, animated: true)
        if(i1 != 0){
            self.sortByDistance()
        }

    }
user3278732
  • 1,694
  • 10
  • 31
  • 67
  • Can you add some more details? Ten seconds does seem a long time before receiving the first update in `didUpdateLocations` What are you using to test your app? Some iPads have no gps chip and have to rely on radio/mobile/cell, etc although this is usually quicker (though less accurate). Do the delays generally occur when you’re inside building/down a mine etc? – Magnas Apr 11 '20 at 07:40
  • I added my viewdidload part. I am running it from my apartment. I am not calling for location update, just straight get user location once. – user3278732 Apr 11 '20 at 07:43
  • Same issue here https://stackoverflow.com/questions/39499541/cllocationmanager-requestlocation-takes-about-10-seconds .. but I don't want to call updatelocation every N seconds..I just want it once..and not interested in accuracy at all – user3278732 Apr 11 '20 at 07:50
  • 1
    You don't call 'didUpdateLocations' yourself. After you request `locationManager.startUpdatingLocation()` the LocationManager will start to call the delegate method `didUpdateLocations` on a continual basis each time it has a new position. I've just plugged my phone into an app I'm developing and the locations start arriving almost instantaneously. Initially the errors are large and with subsequent calls the error decreases. I'd urge you to try it. If you decide to call `locationManager.stopUpdatingLocation()` as soon as you receive the first location, you could do. – Magnas Apr 11 '20 at 08:28
  • Thats what I did, once I got my location I call stopupdatinglocation(). I don't need it to be correct...As long as its in 10 km range error.. – user3278732 Apr 11 '20 at 17:18

0 Answers0