全局变量不是从返回火力地堡数据继承(SWIFT)

问题描述 投票:0回答:1

我想显示一组,我从我的火力点数据库中读取坐标。我通过功能检索纬度和经度坐标,并将其分配给一个全局变量,这样我可以把它显示在我的地图。但不被显示的值时,它显示的原始值(0.0,0.0)。我打印出来我的结果,并将结果分配给全局变量,但它不被继承当我想以显示它。什么是我可以实现或快速修复一些解决方法?

enter image description here

    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.0,0.0)enter image description here

enter image description here

ios swift firebase asynchronous mapkit
1个回答
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)后打印语句,看看哪些打印第一...新的打印语句或完成处理内部的人。

© www.soinside.com 2019 - 2024. All rights reserved.