[英]How to Set UITabBar background image change?
我如何基於與當前位置的距離對數組進行排序並在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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.