我正在尝试显示从Firebase数据库读取的一组坐标。我通过函数检索纬度和经度坐标,并将其分配给全局变量,这样我就可以调用它在地图上显示。但是未显示值,而是显示原始值(0.0,0.0)。我将其打印出结果,并将结果分配给全局变量,但是当我要显示它时不会被继承。我可以实施哪些解决方法或快速解决?
var GPSlatdata = 0.0
var GPSlongdata = 0.0
func getlatitude(completion: @escaping (Double)->()){
let ref = Database.database().reference()
ref.child("GPSData/devices/1/latitude").observeSingleEvent(of: .value) { (snapshot) in
if let latitudefire = snapshot.value as? Double
{
completion(latitudefire)
}
}
}
func getlongitude(completion: @escaping (Double)->()){
let ref = Database.database().reference()
ref.child("GPSData/devices/1/longitude").observeSingleEvent(of: .value) { (snapshot) in
if let longitudefire = snapshot.value as? Double
{
completion(longitudefire)
}
}
}
getlatitude(completion: { (latx) in
print("Before: \(GPSlatdata)")
print("Recieved Lat Value: \(latx)")
GPSlatdata = GPSlatdata + latx
print("GPSlata data is = \(GPSlatdata)")
})
getlongitude(completion: { (longx) in
print("Beforelong: \(GPSlongdata)")
print("Recieved Lat Value: \(longx)")
GPSlongdata = GPSlongdata + longx
print("GPSlata data is = \(GPSlongdata)")
})
let skateboardposition = CLLocationCoordinate2D(latitude: GPSlatdata, longitude: GPSlongdata)
let skateboardposition = CLLocationCoordinate2D(latitude: x1, longitude: x2)
//just added
let skateboardAnnotation = GPSAnnotation(coordinate: skateboardposition, title: "ACES Skaters", subtitle: "Current Board location - \nLatitude: \(GPSlatdata) Longitude: \(GPSlongdata)")
GPSMap.addAnnotation(skateboardAnnotation)
GPSMap.setRegion(skateboardAnnotation.region, animated: true)
更新的更改:仍然无法绘制坐标。
import UIKit
import Foundation
import MapKit
import Firebase
final class GPSAnnotation: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var title: String?
var subtitle: String?
init(coordinate: CLLocationCoordinate2D, title: String?, subtitle: String?)
{
self.coordinate = coordinate
self.title = title
self.subtitle = subtitle
super.init()
}
var region: MKCoordinateRegion{
let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
return MKCoordinateRegion(center: coordinate, span: span)
}
}
extension String {
func toDouble() -> Double? {
return NumberFormatter().number(from: self)?.doubleValue
}
}
class GPSMapViewController: UIViewController {
@IBOutlet weak var GPSMap: MKMapView!
@IBOutlet weak var latLabel: UILabel!
@IBOutlet weak var longLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let queue = DispatchGroup()
// Do any additional setup after loading the view.
var GPSlatdata = 0.0
var GPSlongdata = 0.0
let ref = Database.database().reference()
ref.child("GPSData/devices/1/latitude").observeSingleEvent(of: .value) { (snapshot) in
if let latitudefire = snapshot.value as? Double
{
self.latLabel.text = "\(latitudefire)"
}
}
ref.child("GPSData/devices/1/longitude").observeSingleEvent(of: .value) { (snapshot) in
if let longitudefire = snapshot.value as? Double
{
self.longLabel.text = "\(longitudefire)"
}
}
GPSMap.register(MKMarkerAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
func getlatitude(completion: @escaping (Double)->()){
let ref = Database.database().reference()
ref.child("GPSData/devices/1/latitude").observeSingleEvent(of: .value) { (snapshot) in
if let latitudefire = snapshot.value as? Double
{
completion(latitudefire)
}
}
}
func getlongitude(completion: @escaping (Double)->()){
let ref = Database.database().reference()
ref.child("GPSData/devices/1/longitude").observeSingleEvent(of: .value) { (snapshot) in
if let longitudefire = snapshot.value as? Double
{
completion(longitudefire)
}
}
}
queue.enter()
getlatitude(completion: { (latx) in
print("Before: \(GPSlatdata)")
print("Recieved Lat Value: \(latx)")
GPSlatdata = GPSlatdata + latx
print("GPSlata data is = \(GPSlatdata)")
queue.leave()
})
queue.enter()
getlongitude(completion: { (longx) in
print("Beforelong: \(GPSlongdata)")
print("Recieved Lat Value: \(longx)")
GPSlongdata = GPSlongdata + longx
print("GPSlata data is = \(GPSlongdata)")
queue.leave()
})
DispatchQueue.main.async{
let skateboardposition = CLLocationCoordinate2D(latitude: GPSlatdata, longitude: GPSlongdata)
// let skateboardposition = CLLocationCoordinate2D(latitude: x1, longitude: x2)
print("The skateboard position is: \(skateboardposition)")
print("GPSlatdata is: \(GPSlatdata)")
print("GPSlongdata is: \(GPSlongdata)")
let skateboardAnnotation = GPSAnnotation(coordinate: skateboardposition, title: "ACES Skaters", subtitle: "Current Board location - \nLatitude: \(GPSlatdata) Longitude: \(GPSlongdata)")
self.GPSMap.addAnnotation(skateboardAnnotation)
self.GPSMap.setRegion(skateboardAnnotation.region, animated: true)
}
}
}
extension GPSMapViewController: MKMapViewDelegate{
func GPSMap(_ GPSMap: MKMapView, viewFor annotation: MKAnnotation) -> MKMarkerAnnotationView?{
if let GPSAnnotationView = GPSMap.dequeueReusableAnnotationView(withIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier) as? MKMarkerAnnotationView{
GPSAnnotationView.animatesWhenAdded = true
GPSAnnotationView.titleVisibility = .adaptive
GPSAnnotationView.titleVisibility = .adaptive
}
return nil
}
}
答案 0 :(得分:1)
您不等待完成处理程序完成。完成处理程序不会自动发生-您必须等待数据被检索。尝试使用DispatchGroup
,以便确定事件何时完成。
let queue = DispatchGroup()
for i in 0..<5 {
//Here we add a child into a queue
queue.enter()
print("Entering")
//Start some action and wait for it to finish
//... It finishes
queue.leave()
print("Leaving")
}
queue.notify(queue: .main) {
print("We are done")
}
所以就您而言
let queue = DispatchGroup()
//Start an item
queue.enter()
getlatitude(completion: { (latx) in
print("Before: \(GPSlatdata)")
print("Recieved Lat Value: \(latx)")
GPSlatdata = GPSlatdata + latx
print("GPSlata data is = \(GPSlatdata)")
//we found latdata so leave
queue.leave()
})
//Start an item
queue.enter()
getlongitude(completion: { (longx) in
print("Beforelong: \(GPSlongdata)")
print("Recieved Lat Value: \(longx)")
GPSlongdata = GPSlongdata + longx
print("GPSlata data is = \(GPSlongdata)")
//we found longdata so leave
queue.leave()
})
//We are done with items once they BOTH finish.
queue.notify(queue: .main) {
let skateboardposition = CLLocationCoordinate2D(latitude: GPSlatdata, longitude: GPSlongdata)
//just added
let skateboardAnnotation = GPSAnnotation(coordinate: skateboardposition, title: "ACES Skaters", subtitle: "Current Board location - \nLatitude: \(GPSlatdata) Longitude: \(GPSlongdata)")
GPSMap.addAnnotation(skateboardAnnotation)
GPSMap.setRegion(skateboardAnnotation.region, animated: tr
}
参考文献:https://www.raywenderlich.com/5371-grand-central-dispatch-tutorial-for-swift-4-part-2-2
注意:要测试这种情况,请在let skateboardposition = CLLocationCoordinate2D(latitude: GPSlatdata, longitude: GPSlongdata)
之后放置一条打印语句,然后查看哪些打印语句首先打印...新的打印语句或完成处理程序中的打印语句。