我正在尝试将用户的当前位置保存到Firebase中,其中包括时间和地址。基本上,我的代码可以正常工作,但是仅第一个位置地址始终显示“标签”,而不显示地址。因此,当我重新运行该应用程序时,它在Firebase实时数据库中始终具有标签数据。我不知道该如何解决。我在下面附加了代码和数据库图片。
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if locations.count > 0 {
let location = locations[locations.count-1]
let maxAge:TimeInterval = 60;
let requiredAccuracy:CLLocationAccuracy = 80;
let locationIsValid:Bool = Date().timeIntervalSince(location.timestamp) < maxAge && location.horizontalAccuracy <= requiredAccuracy;
if locationIsValid
{
NSLog(",,, location : %@",location);
NSLog("valid locations.....");
}
}
let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: locations[0].coordinate.latitude, longitude: locations[0].coordinate.longitude), span: MKCoordinateSpan(latitudeDelta: 0.005, longitudeDelta: 0.005))
self.mapView.setRegion(region, animated: true)
CLGeocoder().reverseGeocodeLocation(manager.location!) { (placemarks, error) in
if error != nil{
print("Error: " + (error?.localizedDescription)!)
return
}
let pm = placemarks! as [CLPlacemark]
if pm.count > 0 {
let pm = placemarks![0]
let subThoroughfare = pm.subThoroughfare ?? ""
let thoroughfare = pm.thoroughfare ?? ""
let locality = pm.locality ?? ""
let administrativeArea = pm.administrativeArea ?? ""
let postalCode = pm.postalCode ?? ""
let country = pm.country ?? ""
let address = (subThoroughfare)+" "+(thoroughfare)+" "+(locality)+","+(administrativeArea)+" "+(postalCode)+" "+(country)
self.myAddress.text = "\(address)"
}
}
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd, HH:mm:ss"
formatter.timeZone = NSTimeZone.local
let timestampFormattedStr = formatter.string(from: (locationManager.location?.timestamp)!)
let timeZone = NSTimeZone(forSecondsFromGMT: NSTimeZone.local.secondsFromGMT())
let timeZoneName = timeZone.localizedName(.standard, locale: NSLocale.current)
let timestampWithTimeZone = "\(timestampFormattedStr) \(String(describing: timeZoneName))"
let values = ["Time": timestampWithTimeZone,"Address": myAddress.text as Any]
let ref = Database.database().reference()
ref.child("userLocations").childByAutoId().setValue(values)
}
答案 0 :(得分:1)
reverseGeocodeLocation
块是异步的。这意味着您的Firebase代码在调用该块之前发生。分配self.myAddress
时,您已提交到数据库。要解决此问题,请将firebase代码放入代码块中。
if pm.count > 0 {
let pm = placemarks![0]
let subThoroughfare = pm.subThoroughfare ?? ""
let thoroughfare = pm.thoroughfare ?? ""
let locality = pm.locality ?? ""
let administrativeArea = pm.administrativeArea ?? ""
let postalCode = pm.postalCode ?? ""
let country = pm.country ?? ""
let address = (subThoroughfare)+" "+(thoroughfare)+" "+(locality)+","+(administrativeArea)+" "+(postalCode)+" "+(country)
self.myAddress.text = "\(address)"
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd, HH:mm:ss"
formatter.timeZone = NSTimeZone.local
let timestampFormattedStr = formatter.string(from: (locationManager.location?.timestamp)!)
let timeZone = NSTimeZone(forSecondsFromGMT: NSTimeZone.local.secondsFromGMT())
let timeZoneName = timeZone.localizedName(.standard, locale: NSLocale.current)
let timestampWithTimeZone = "\(timestampFormattedStr) \(String(describing: timeZoneName))"
let values = ["Time": timestampWithTimeZone,"Address": myAddress.text as Any]
let ref = Database.database().reference()
ref.child("userLocations").childByAutoId().setValue(values)
}
}
答案 1 :(得分:0)
遵循以下代码:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
manager.stopUpdatingLocation()
if let location = locations.last {
latitudeLabel.text = "Latitude: " + "\(location.coordinate.latitude)"
longitudeLabel.text = "Longitude: " + "\(location.coordinate.longitude)"
let geoCoder = CLGeocoder()
geoCoder.reverseGeocodeLocation(location) { (placemarks, error) in
if error != nil {
print("Error found while reverse GEO coding.")
return
}
if let placemark = placemarks?.first {
var fullAddress = ""
if let name = placemark.name {
fullAddress.append("Name: " + name)
}
if let country = placemark.country {
fullAddress.append("\nCountry: " + country)
}
if let countryCode = placemark.isoCountryCode {
fullAddress.append("\nCountry code: " + countryCode)
}
if let locality = placemark.locality {
fullAddress.append("\nLocality: " + locality)
}
if let postal = placemark.postalCode {
fullAddress.append("\nPostal: " + postal)
}
if let thoroughfare = placemark.thoroughfare {
fullAddress.append("\nThoroughfare: " + thoroughfare)
}
if let administrativeArea = placemark.administrativeArea {
fullAddress.append("\nAdministrativeArea: " + administrativeArea)
}
if let subLocality = placemark.subLocality {
fullAddress.append("\nSubLocality: " + subLocality)
}
}
}
}
}
您必须停止更新位置。
希望您遇到问题了,此代码将为您工作。