Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
swift 3计算到当前位置的距离,并将结果从壁橱排序到最远位置_Swift_Tableview_Cllocation - Fatal编程技术网

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

swift 3计算到当前位置的距离,并将结果从壁橱排序到最远位置,swift,tableview,cllocation,Swift,Tableview,Cllocation,我试图计算从事件到当前位置的距离,对结果进行排序并将其填充到tableview中。我不断得到错误的可选展开值距离为零 private func observeEvents() { refHandle = ref.observe(.childAdded, with: { (snapshot) -> Void in let eventDetails = snapshot.value as! Dictionary<String, AnyObject>

我试图计算从事件到当前位置的距离,对结果进行排序并将其填充到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

我终于找到了答案。问题是在那里异步调用distance函数,因为结果总是nil。我为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")
                }

            }
        }
    })
refHandle=ref.observe(.childAdded,带有:{(快照)->Void in
将location=event[“address”]设为!字符串
getCoordinates(地址:location!){lat,long,中有错误
如果错误!=nil{
打印(“错误”)
}否则{
自纬度=纬度
self.longitude=long
让距离=CLLocation(纬度:self.lation!,经度:self.longitude!)。距离(from:self.currentLocation!)
如果让name=eventDetails[“eventName”]作为!String!,name.characters.count>0
{
self.events.append(事件(id:eventID,name:name,location:location!、dateTime:dateTime!、addedByUser:addedByUser!、主治医师:主治医师!、距离:距离))
self.events.sort(按:{$0.distance<$1.distance})
self.tableView.reloadData()
}否则{
打印(“错误!无法从数据库加载事件”)
}
}
}
})
    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")
                }

            }
        }
    })