代码之家  ›  专栏  ›  技术社区  ›  mninety5

在表格上显示距离用户最近的位置列表

  •  1
  • mninety5  · 技术社区  · 7 年前

    这是我显示位置的方式:

    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 回复  |  直到 7 年前
        1
  •  2
  •   jvrmed    7 年前

    现在需要做的是排序 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
    }
    

    而且 sort sorted 取决于你是否愿意 就地排序 或者创造一个 已排序副本 . 有关CLLocationManager工作原理的更多信息,请阅读 here

        2
  •  1
  •   brandonscript    7 年前

    使用您的 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
    ]
    */