Службы определения местоположения работают в фоновом режиме?Я заметил, что когда я читаю местоположение из менеджера местоположений в глобальную переменную, отправляемую на сервер синтаксического анализа, оно не отражает данные о местоположении после вставки.Я подозреваю, что это потому, что почтовый код выполняется до того, как функция менеджера местоположения устанавливает переменные, потому что он работает в фоновом режиме.
Есть ли способ запустить менеджер местоположений, если просмотр загружен, но убедитесь, что View загрузил, ждет, пока службы определения местоположения не закончили свое первое обновление?
// VIEW DID LOAD
override func viewDidLoad() {
super.viewDidLoad()
self.imagePicker.delegate = self
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
var secondsFromGMT: Int { return TimeZone.current.secondsFromGMT()
// -----------------------
// ? Wait for LOCATION SERVICES?? if placeIsSet {}
// -----------------------
let post = PFObject(className: "BlipPost")
// need struct2Parse() function
post["user_id"] = curBlip.user_id
post["blip_msg"] = curBlip.blip_note
post["blip_date"] = curBlip.blip_dt
post["TZOffset_seconds"] = curBlip.blip_tz_secs
post["blip_address"] = curBlip.blip_addr
post["latitude"] = curBlip.blip_lat
post["longitude"] = curBlip.blip_lon
post["IsPublic"] = curBlip.isPublic
print("location set about to post= \(locationSet)")
post.saveInBackground { (success, error) in
if success {
print("location set success= \(self.locationSet)")
print("Blip instantiated")
curBlip.blip_id = post.objectId!
// Should we add the new blip now or wait on "Save"
} else {
print("\(error?.localizedDescription ?? "")")
}
}
// LOCATION MANAGER
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let addrDelim = " "
let userLocation: CLLocation = locations[0]
currLocation = userLocation
let latitude = userLocation.coordinate.latitude
let longitude = userLocation.coordinate.longitude
location.lat = latitude
location.lon = longitude
location.strLatitude = String(format: "%.8f", latitude)
location.strLongitude = String(format: "%.8f", longitude)
location.strCourse = String(userLocation.course)
location.strSpeed = String(userLocation.speed)
location.strAltitude = String(userLocation.altitude)
location.strLatLon = location.strLatitude + ", " + location.strLongitude
locationSet = true
// Reverse Geo Code for addr
CLGeocoder().reverseGeocodeLocation(userLocation) { (placemarks, error) in
if error != nil {
print(error!)
} else {
if let placemark = placemarks?[0] {
var address = ""
if placemark.subThoroughfare != nil {
address += placemark.subThoroughfare! + " "
self.location.subThoroughfare = placemark.subThoroughfare!
}
if placemark.thoroughfare != nil {
address += placemark.thoroughfare! + addrDelim
self.location.thoroughfare = placemark.thoroughfare!
}
if placemark.subLocality != nil {
address += placemark.subLocality! + addrDelim
self.location.subLocality = placemark.subLocality!
}
if placemark.subAdministrativeArea != nil {
address += placemark.subAdministrativeArea! + addrDelim
self.location.subAdministrativeArea = placemark.subAdministrativeArea!
}
if placemark.postalCode != nil {
address += placemark.postalCode! + addrDelim
self.location.postalCode = placemark.postalCode!
}
if placemark.country != nil {
address += placemark.country! + addrDelim
self.location.country = placemark.country!
}
self.location.strAddress = address
print("location set in Manager= \(self.locationSet)")
print(self.location.strAddress)
}
}
}
// Stop updating location to save battery
locationManager.stopUpdatingLocation()
}