swift 3计算到当前位置的距离并将壁橱中的结果排序到最远

时间:2017-03-24 05:18:51

标签: swift tableview cllocation

我试图计算从事件到当前位置的距离,对结果进行排序并在tableview中填充结果。我一直收到错误,因为可选的展开值距离为零。

     private func observeEvents() {
    refHandle = ref.observe(.childAdded, with: { (snapshot) -> Void in
        let eventDetails = snapshot.value as! Dictionary<String, AnyObject>
        let eventID = snapshot.key
        let location = eventDetails["location"] as! String!

        //calculating distance
        self.forwardGeocoding(address: location!)
        let distance = self.eventLocation?.distance(from: self.currentLocation!) as Double!
        //end calculating

        let dateTime = eventDetails["dateTime"] as! String!
        let addedByUser = eventDetails["addedByUser"] as! String!
        let attending = eventDetails["attendance"] as! String!
        if let name = eventDetails["eventName"] as! String! , name.characters.count > 0
        {

            self.events.append(Events(id:eventID, name: name, location: location!, dateTime: dateTime!, addedByUser: addedByUser!, attending: attending! , distance: distance!))
            self.events.sort(by: { $0.distance < $1.distance})

            self.tableView.reloadData()
        } else {
            print("Error ! Can't load events from database")
        }
    })
} //load events data to uitableview

我创建了一个从地址

返回CLLocation的函数
      func forwardGeocoding(address: String) {
    CLGeocoder().geocodeAddressString(address, completionHandler: { (placemarks, error) in
        if error != nil {
            print(error!)
            return
        }
        if (placemarks?.count)! > 0 {
            let placemark = placemarks?[0]
            self.eventLocation = placemark?.location
        }
    })
}

1 个答案:

答案 0 :(得分:0)

我终于找到了答案。问题是距离的函数是异步调用的,因为结果总是为零。我为forwardGeocoding函数创建了一个完成处理程序,用于从地址字符串返回纬度和经度,并在嵌套的firebase侦听器中调用结果。这是代码,我希望如果有人遇到类似的问题,我会发现它有用。

  //Get lat and long

func getCoordinates(address: String, completionHandler: @escaping (_ lat: CLLocationDegrees?, _ long: CLLocationDegrees?, _ error: Error?) -> ()) -> Void {

    var _:CLLocationDegrees
    var _:CLLocationDegrees
    let geocoder = CLGeocoder()
    geocoder.geocodeAddressString(address) { (placemarks: [CLPlacemark]!, error: Error!) in

        if error != nil {

            print("Geocode failed with error: \(error.localizedDescription)")

        } else if placemarks.count > 0 {

            let placemark = placemarks[0] as CLPlacemark
            let location = placemark.location

            let lat = location?.coordinate.latitude
            let long = location?.coordinate.longitude

            completionHandler(lat, long, nil)
        }
    }
}

在firebase侦听器中嵌套调用

    refHandle = ref.observe(.childAdded, with: { (snapshot) -> Void in
    let location = event["address"] as! String          
    self.getCoordinates(address: location!) { lat, long, error in
            if error != nil {
                print("Error")
            } else {
                self.latitude = lat
                self.longitude = long
                let distance = CLLocation(latitude: self.latitude!,longitude: self.longitude!).distance(from: self.currentLocation!)

                if let name = eventDetails["eventName"] as! String! , name.characters.count > 0
                {

                    self.events.append(Events(id:eventID, name: name, location: location!, dateTime: dateTime!, addedByUser: addedByUser!, attending: attending!, distance: distance))

                    self.events.sort(by: { $0.distance < $1.distance})

                    self.tableView.reloadData()
                } else {
                    print("Error ! Can't load events from database")
                }

            }
        }
    })