如何设置UITabBar背景图像更改?

时间:2017-07-12 07:03:19

标签: ios objective-c

我如何根据与当前位置的距离对数组进行排序并在tableview中显示。当我使用排序时没有得到任何正确的结果,我得到的数组有随机distance.can任何人指导我解决这个问题

1 个答案:

答案 0 :(得分:1)

要以最佳方式根据与当前位置的距离对位置进行排序,将具有结构形式的位置点

struct LocationPoints {
    var latitude: CLLocationDegrees
    var longitude: CLLocationDegrees

    func distance(to currentLocation: CLLocation) -> CLLocationDistance {
        return CLLocation(latitude: self.latitude, longitude: self.longitude).distance(from: currentLocation)
    }
}

假设您有一个具有纬度和幅度的LocationPoints数组。经度

var coordinates: [LocationPoints] = []
            coordinates.append(LocationPoints(latitude: Double(25), longitude: Double(24)))
                coordinates.append( LocationPoints(latitude: Double(23), longitude: Double(22)))

排序功能

    coordinates = sortLocationsWithCurrentLocation(locations: coordinates, currentLocation: CLLocation(latitude: Double(20), longitude: Double(21)))




    func sortLocationsWithCurrentLocation(locations:[LocationPoints],currentLocation:CLLocation) -> [LocationPoints] {
//set here current position as current location
            let currentPosition : CLLocation = CLLocation(latitude: 30, longitude: 24)
            let sortedLocations = locations.sorted(by: { (point1 : LocationPoints, point2 :LocationPoints) -> Bool in
                if point1.distance(to: currentPosition) < point2.distance(to: currentPosition)
                {
                    return true
                }
               return false
            })
            return sortedLocations
        }