2017-10-16 85 views
1

我一直在尝试显示tableView上的距离,但我无法让它发生。这个问题跟随这个问题:CLLocationDistance conversion。我检查了距离。在我Location类使用此功能:显示与mapKit Swift用户的距离(英里/公里)

// 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) 
    } 
} 
+0

您的问题未提供足够的信息。您在表格视图中显示的数组中的条目是什么?不同对象的CLLocation位置?你是否应该计算一系列位置和用户当前位置之间的距离?你的函数'distance()'如何被调用?请编辑您的问题以提供有关上述信息。 –

+0

你在'LocationManager.shared.location'中有什么?在我看来,你使用它而不是'lastUserLocation'做你的比较 – jvrmed

+0

@JavierMedina请看我的更新的问题 – mninety5

回答

2

考虑到你的代码,我想提出一些假设:

  1. sortedLocations阵列具有您从JSON或提取的不同位置随你。
  2. 在加载您的数据之前,您可以拨打startUpdatingLocation()或其他地方。
  3. 您是在您的didUpdateLocations中收到更新
  4. 您的LocationManager会将您所有位置的订单副本保存在名为locations的变量中,您在didUpdateLocations内订购的变量。

即认为,你想要做什么,我理解是为了显示你的sortedLocations下令根据参考位置

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

  1. 只加载您UITableView一旦您已经在第一用户位置由didUpdateLocations检索。
  2. 一旦获得新位置,通过在didUpdateLocations内调用tableView.reloadData()即可更新UITableView。这会在您每次收到位置更新时重新绘制列表,并按位置对其进行排序。

然而,在任何你所需要的情况下更换您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 
    } 
} 

使用您当前的代码,您将所有距离与变量显然没有被初始化。

+0

我的目标是强制UITableView更新,一旦你得到一个新的位置,所以它不断更新。 – mninety5

+0

然后调用reloadData并修复你的距离函数应该已经足够了 – jvrmed

+0

我试过了,并且还定义了它'var lastUserLocation:CLLocation?',但'self.lastUserLocation'没有 – mninety5

相关问题