1

我一直试图在 tableView 上显示距离,但我无法让它发生。这个问题是从这个问题开始的:CLLocationDistance conversion。我检查了距离。Location在我的课堂上使用这个功能:

// Get distance
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)

    // Add a lastUserLocation to LocationManager and update it every time that the delegate receives a new location
    LocationManager.shared.lastUserLocation = locations.last
    LocationManager.shared.sortLocationsInPlace()

    self.mapView.showsUserLocation = true

}

LocationManager 中的排序功能:

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

func sortLocationsInPlace() {
    if let validLocation = lastUserLocation {
        locations.sort { (l1, l2) -> Bool in
            return l1.distance(to: validLocation) < l2.distance(to: validLocation)
        } 
    }
}

cellForRowAt:

var sortedLocations = [Location]()

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
}

更新

课内Location

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)
    }
}
4

1 回答 1

2

考虑到你的代码,我做了一些假设:

  1. 您的数组具有从JSON或其他内容sortedLocations中提取的不同位置。
  2. 在加载数据之前,您可以在某处调用startUpdatingLocation()或类似方法。
  3. 您在. _didUpdateLocations
  4. LocationManager将所有位置的有序副本保存在一个名为的变量中locations,即您在其中订购的那个didUpdateLocations

考虑到,我了解您想要做的是sortedLocations根据参考位置显示您的订单。

UITableView缺少的是在收到您的用户位置后更新您的数据。您有两个主要选择:

  1. UITableView仅在您已经检索到您的第一个用户位置后才加载您的didUpdateLocations
  2. UITableView要在获得新位置后强制更新,请调用tableView.reloadData()inside didUpdateLocations。这将在您每次收到位置更新时重新绘制您的列表,并按位置对其进行排序。

但是,在任何这些情况下,您都需要替换cellForRow文本以显示您的距离,而不是location.name

// Distance in meters
cell.textLabel?.text = String(location.distance(to: LocationManager.shared.lastUserLocation!))

// Distance in miles
cell.textLabel?.text = String(location.distance(to: LocationManager.shared.lastUserLocation!)*0.00062137)

更新您的didUpdateLocations

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

        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)

        // Add a lastUserLocation to LocationManager and update it every time that the delegate receives a new location
        LocationManager.shared.lastUserLocation = location
        LocationManager.shared.sortLocationsInPlace()

        sortedLocations = LocationManager.shared.locations
        tableView.reloadData()

        self.mapView.showsUserLocation = true
    }
}

使用您当前的代码,您正在将所有距离与一个self.location显然没有在任何地方初始化的变量进行比较。

于 2017-10-16T12:42:14.760 回答