distanceInMeters数组和sorting问题

我想按距离sorting我的TableView,但我已经混合:(请帮助我,如果你可以,我将不胜感激,真的:(

我有一个100个对象的数组。

var malls: [Mall] = [ Mall(name: "Европейский", type: "торговый центр", image: "europe.jpg", time: "с 10.00 до 22.00", timeWeek: "с 10.00 до 23.00", city: "Москва", location: "Киевская", adress: "г. Москва, Киевского вокзала площадь, д.2", cinemaName:"formulakino.png", productName: "perekrestok.png", numShop: "309", website: "http://europe-tc.ru", schemeWeb:"http://www.europe-tc.ru/shops/", longitude:37.566, latitude:55.745), Mall(name: "Золотой Вавилон Ростокино", type: "торговый центр", image: "vavilon.jpg", time: "с 10.00 до 22.00", timeWeek: "с 10.00 до 22.00", city: "Москва", location: "Проспект Мира", adress: "г. Москва, Проспект Мира, д. 211", cinemaName: "Люксор", productName: "okay.png", numShop: "280", website: "http://zolotoy-vavilon.ru/rostokino", schemeWeb:"http://www.zolotoy-vavilon.ru/rostokino/map", longitude:37.663, latitude:55.846), ] 

等等..对西里尔很抱歉

和覆盖func tableView后,我有这个

 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { let currentLocation = locations[0] if (currentLocation.horizontalAccuracy > 0 ) { locationManager.stopUpdatingLocation() let coords = CLLocation(latitude: currentLocation.coordinate.latitude, longitude: currentLocation.coordinate.longitude) **let mallLocate = CLLocation(latitude: mall.latitude, longitude: mall.longitude)** let distanceInMeters = mallLocate.distance(from: coords) print (distanceInMeters) } } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! EateriesTableViewCell let mall = mallToDisplayAt(indexPath: indexPath) cell.thumbnailImageView.image = UIImage(named: mall.image) cell.thumbnailImageView.clipsToBounds = true cell.nameLabel.text = mall.name cell.locationLabel.text = mall.location cell.typeLabel.text = mall.time return cell } 

我在这一行有问题

 let mallLocate = CLLocation(latitude: mall.latitude, longitude: mall.longitude) 

使用未parsing的标识符“mall”

我该如何解决? 以及如何按方向sorting? 非常感谢。

使用未parsing的标识符“mall”

=>函数locationManager didUpdateLocations的作用域中没有任何名称为“mall” 。 含义: locationManager didUpdateLocations不知道在tableView cellForRowAt indexPath发生了什么,反之亦然。


你可以通过下面的步骤来解决这个问题:

  1. 从中移动距离计算
    locationManager didUpdateLocations

    tableView cellForRowAt indexPath
  2. 添加一个属性到你的类,存储当前位置:
    var currentPosition: CLLocation? = nil

  3. 将您在locationManager didUpdateLocations收到的locationManager didUpdateLocationscurrentPosition属性

  4. 添加self.tableView.reloadData()后收到并分配的位置
  5. 使用currentPositioncurrentPosition中的currentPosition来计算距离并更新distanceLabel
  6. 请记住, currentPosition可能nil ,距离无法计算=>使标签为空或为其添加“未知距离”之类的东西

你必须select你的数组的索引:

 let mallLocate = CLLocation(latitude: malls[0].latitude, longitude: malls[0].longitude) 

这听起来像是你想根据他们到用户的当前位置的距离来分类你的商场arrays。

我build议你添加一个var distanceToUser到Mall对象。 (使其成为一个可选的双)

locationManager.didUpdateLocations() ,遍历商场数组,并使用CLLocation distance(from:)函数计算从新用户位置到每个商场的distanceToUser ,并将该值存储在每个商场的distanceToUser属性中。

一旦填充了商场数组的distanceToUser属性,就可以通过该属性对数组进行sorting。 最后,调用你的表视图的reloadData()方法。 写你的tableView(cellForRowAt:)方法来显示您的单元格在现有的数组顺序。

在商城对象中包含距离属性的原因是性能。 用于计算两点之间距离的位置pipe理器function实际上是相当费时的(为了在地球表面上获得准确的计算,必须进行3D三angular测量)。您希望避免一次又一次地计算距离值。 相反,通过批量计算到新更新位置的距离,sorting可以将距离值视为已知的常量,当位置发生变化时,每个Mall对象计算一次。

如果你有很多商场对象,你可能需要在后台线程中进行距离计算,然后调用主线程在计算完成后重新载入你的表视图,但是你可以编写你的计算代码在主线程上运行然后回去重构它,如果你发现你的用户界面滞后时,有一个位置更新。

您还需要将位置pipe理器configuration为只在当前位置发生相当大的变化时更新。 要么这样做,要么把代码比较新的位置和旧的只有重新sorting商场数组,如果距离已经改变足够影响你的商场arrays的sorting顺序。 (在最高的灵敏度读数上,位置pipe理器给你一个几乎不变的微小位置变化,这意味着你正在重新计算你的距离,并不断重新排列你的商场arrays。