Firebase地图检索纬度和经度

时间:2017-01-03 18:31:57

标签: ios swift firebase firebase-realtime-database

我有这个地图控制器,我想获得纬度和经度坐标来对地图进行注释。出于某种原因,我不知道如何正确设置异步调用,并且从数据库中找不到任何内容。

将坐标存储为双精度的最佳方法是什么?

var restaurantArray = [Restaurant]()


@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var segments: UISegmentedControl!

let locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()
    fetchRestaurants()
    print(restaurantArray.count)
    title = "Maps"
    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Main Menu", style: .plain, target: self, action: #selector(SSASideMenu.presentLeftMenuViewController))
    self.locationManager.delegate = self//as soon as loaded find location--conforms to delegate
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest//best location
    self.locationManager.requestWhenInUseAuthorization()//only want location when using app
    self.locationManager.startUpdatingLocation()//turn on location manager..make location start looking
    self.mapView.showsUserLocation = true//shows blue dot

}



func fetchRestaurants(){
    FIRDatabase.database().reference().child("AthensRestaurants/Restaurants").observe(.value, with: { (snapshot) in
        var results = [Restaurant]()

        for res in snapshot.children{
            let res = Restaurant(snapshot: res as! FIRDataSnapshot)
            print(res.address)
            results.append(res)
        }
        self.restaurantArray = results
    })
}





//MARK: - Location Delegate Methods
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {//didupdate is contiously called so below is continuously called
    let location = locations[0]
    let span = MKCoordinateSpanMake(0.01, 0.01)
    let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
    let region = MKCoordinateRegionMake(myLocation, span)//lat long--region that we want map to scope to--parameters is closeness zoom
    self.mapView.setRegion(region, animated: true)//since we have thise we can stop updating eventually
    self.locationManager.stopUpdatingLocation()

}

//check for errors
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {//should be NSError but
    print("Errors:" + error.localizedDescription)
}
//segment changer for terrain, hybrid, and regular
@IBAction func segChange(_ sender: Any) {
    switch segments.selectedSegmentIndex {
    case 0:
        mapView.mapType = MKMapType.standard
        break
    case 1:
        mapView.mapType = MKMapType.satellite
        break
    case 2:
        mapView.mapType = MKMapType.hybridFlyover
        break
    default:
        break
    }

}

}

1 个答案:

答案 0 :(得分:1)

Firebase是异步的,需要有时间加载数据。即代码比互联网快。

当在viewDidLoad中调用fetchRestaurants()时,Firebase会检出数据,并且在闭包内返回时该数据只有效。在你的情况下发生的是print(restaruantArray.count)在Firebase有机会返回数据并填充数组之前执行的方式。

需要发生的是,在闭包内,通过遍历快照来填充数组,这就是你正在做的事情,然后在for循环完成之后重新加载你的tableView(再次,在闭包内)。

哦,你不需要在闭包中创建单独的结果数组,然后将它分配给restaurantArray。只需直接填充restaurantArray。