在表

时间:2017-10-12 22:17:52

标签: ios swift sorting cllocationmanager

我可以在桌子上显示位置列表。但现在我想根据当前位置对其进行排序。

这是我显示位置的方式:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "locationCell", for: indexPath)

    let location = LocationManager.shared.locations[indexPath.row]
    cell.textLabel?.text = location.name
    return cell
}

我试图在Location课程中实现这个距离,这是我从https://stackoverflow.com/a/35200027/6362735获得的,但我不确定接下来需要做什么以及如何对其进行排序。

这是我的位置类:

class Location {
    var name: String
    var latitude: Double
    var longitude: Double
    var location:CLLocation {
        return CLLocation(latitude: latitude, longitude: longitude)
    }

    init?(json: JSON) {
        guard let name = json["name"] as? String, let latitude = json["latitude"] as? Double, let longitude = json["longitude"] as? Double else { return nil }
        self.name = name
        self.latitude = latitude
        self.longitude = longitude
    }

    func distance(to location: CLLocation) -> CLLocationDistance {
        return location.distance(from: self.location)
    }
}

显示当前位置:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = locations[0]

    let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
    let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
    let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
    mapView.setRegion(region, animated: true)

    self.mapView.showsUserLocation = true
}

2 个答案:

答案 0 :(得分:2)

您现在需要做的是按距离对LocationManager.shared.locations进行排序,并将用户的位置作为参数传递。您可以将这两个放在LocationManager

func getSortedLocations(userLocation: CLLocation) -> [Location] {
    return locations.sorted { (l1, l2) -> Bool in
        return l1.distance(to: userLocation) < l2.distance(to: userLocation)
    }
}

func sortLocationsInPlace(userLocation: CLLocation) {
    locations.sort { (l1, l2) -> Bool in
        return l1.distance(to: userLocation) < l2.distance(to: userLocation)
    }
}

对位置进行排序后,请致电tableView.reloadData(),您的行应按距离排序。

此代码的使用位置取决于您的应用的结构。

如果您使用按钮过滤,则可以对操作中的位置进行排序:

@IBAction func orderByDistance() {
    sortLocationsInPlace(userLocation: yourUsersLocation)
    tableView.reloadData()
}

如果您希望始终订购数据,则可以在首次创建tableView时对其进行排序。在UIViewController

let sortedLocations: [Location]()

override func viewDidLoad() {
    sortedLocations = getSortedLocations(userLocation: yourUsersLocation)
}

然后您可以更改dataSource方法:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "locationCell", for: indexPath)

    let location = sortedLocations[indexPath.row]
    cell.textLabel?.text = location.name
    return cell
}

,请记住,您可以使用sortsorted,具体取决于您是否要排序或创建排序副本。有关CLLocationManager如何工作的更多信息,请阅读here

答案 1 :(得分:0)

使用distance函数按left < right对结果数组进行排序:

locations.sorted(by: { $0.distance(to: myLocation) < $1.distance(to: myLocation) } )

这是一个可以测试的工作示例:

import Foundation
import CoreLocation

struct Location {

    var latitude: Double
    var longitude: Double
    var location:CLLocation {
        return CLLocation(latitude: latitude, longitude: longitude)
    }

    init(lat: Double, long: Double) {
        self.latitude = lat
        self.longitude = long
    }

    func distance(to location: CLLocation) -> CLLocationDistance {
        return location.distance(from: self.location)
    }
}

let locations: [Location] = [
    Location(lat: 61.98573, long: 27.57300),
    Location(lat: -62.98404, long: 62.81190),
    Location(lat: -3.18446, long: 107.07900)]

let myLocation = CLLocation(latitude: 73.30051, longitude: -141.88647)

let locationsClosestToMe = locations.sorted(by: { $0.distance(to: myLocation) < $1.distance(to: myLocation) } )

证明功能(单元测试):

print(locationsClosestToMe.map { $0.distance(to: myLocation) } )

/*
[
  4970593.6601553941, 
  11003159.607318919, 
  18486409.053517241
]
*/