gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:17:14 25 4
gpt4 key购买 nike

我如何根据距当前位置的距离对数组进行排序并在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com