ios - 如何设置UITabBar背景图片变化?

标签 ios objective-c

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

最佳答案

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

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
        }

关于ios - 如何设置UITabBar背景图片变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45050640/

相关文章:

ios - 显示 : flex; doesn't work on iPad (both Chrome and Safari)

objective-c - UITableViewController的表调整大小

ios - 解析 Apns 负载

objective-c - 如何将此方法从 Objective-C 转换为 Swift

ios - 将链接语法转换为嵌入在文本 UITextView 中的链接

ios - 在 objective-c 中声明 float

boolean 值的Objective-c dealloc

ios - Facebook-由app_scoped_user_id打开个人资料

iOS - 从系统设置运行应用深层链接

objective-c - 无法在我的 iPad 模拟器中显示正确的 URL