ios - Swift Firebase 按距离排序

标签 ios arrays swift firebase tableview

我正在尝试按距离对数组进行排序。我已经把所有东西都连接起来以获取距离,但不确定如何从距离用户位置最近到最远进行排序。我已将以下代码用于 MKMapItem,但不确定如何应用于我当前的数组。

func sortMapItems()  {
                    self.mapItems = self.mapItems.sorted(by: { (b, a) -> Bool in
                        return self.userLocation.location!.distance(from: a.placemark.location!) > self.userLocation.location!.distance(from: b.placemark.location!)
                    })
                }

Firebase 调用

databaseRef.child("Businesses").queryOrdered(byChild: "businessName").observe(.childAdded, with: { (snapshot) in

        let key = snapshot.key

        if(key == self.loggedInUser?.uid) {
            print("Same as logged in user, so don't show!")
        } else {
            if let locationValue = snapshot.value as? [String: AnyObject] {
                let lat = Double(locationValue["businessLatitude"] as! String)
                let long = Double(locationValue["businessLongitude"] as! String)
                let businessLocation = CLLocation(latitude: lat!, longitude: long!)

                let latitude = self.locationManager.location?.coordinate.latitude
                let longitude = self.locationManager.location?.coordinate.longitude
                let userLocation = CLLocation(latitude: latitude!, longitude: longitude!)

                let distanceInMeters : Double = userLocation.distance(from: businessLocation)
                let distanceInMiles : Double = ((distanceInMeters.description as String).doubleValue * 0.00062137)
                let distanceLabelText = "\(distanceInMiles.string(2)) miles away"

                var singleChildDictionary = locationValue
                singleChildDictionary["distanceLabelText"] = distanceLabelText as AnyObject
                self.usersArray.append(singleChildDictionary as NSDictionary)

                /*
                func sortMapItems()  {
                    self.mapItems = self.mapItems.sorted(by: { (b, a) -> Bool in
                        return self.userLocation.location!.distance(from: a.placemark.location!) > self.userLocation.location!.distance(from: b.placemark.location!)
                    })
                }
                */

            }


            //insert the rows
            self.followUsersTableView.insertRows(at: [IndexPath(row:self.usersArray.count-1,section:0)], with: UITableViewRowAnimation.automatic)
        }

    }) { (error) in
        print(error.localizedDescription)
    }
}

最佳答案

首先在您的代码中进行这些更改

singleChildDictionary["distanceInMiles"] = distanceInMiles

然后你可以这样排序:

self.usersArray = self.usersArray.sorted {
        !($0["distanceInMiles"] as! Double > $1["distanceInMiles"] as! Double)
}

关于ios - Swift Firebase 按距离排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45866712/

相关文章:

iOS/iPhone SDK : Is there an event for when network is lost/back?

ios - Objective-C:在字符串中查找和替换

ios - 控制标记的信息窗口

c - 由于大小分配导致的数组错误

session 数组中的php Post数组

swift 无法为索引为 'String' 的 [String : DataModelBase. Type] 类型的值添加下标

ios - 如果我将 UUID 存储在钥匙串(keychain)中,iOS 应用商店批准是否有任何问题

javascript - 如何区分数组和数组的数组

swift - Firebase/Swift 查询顺序然后匹配值

ios - 这个函数/变量是如何工作的?