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()
}
}