2017-08-11 125 views
0

在xCode中,我嘗試對TableView內的對象進行排序,以便它們按照與用戶的接近程度排列。我看到這個問題,但我似乎無法弄清楚如何將它實現到我的項目中,因爲我通過JSON文件解析位置。TableView根據距離當前位置的距離進行排序(解析來自JSON的位置)

我需要做什麼來計算用戶的位置和每個位置對象的位置之間的距離?那麼我怎麼才能按升序對TableView中的對象進行排序呢?

這裏是Location.swift型號:

import Foundation 
import MapKit 
import CoreLocation 

var currentLocation:CLLocation? 

class Location: NSObject { 
    var id: String = "" 
    var name: String = "" 
    var type: String = "" 
    var location: String = "" 
    var image: String = "" 
    var activity: String = "" 
    var rating: String = "" 
    var latitude: Double = 0.0 
    var longitude: Double = 0.0 
    var distance: Double { 
     get { 
      return CLLocation(latitude: latitude, longitude: longitude).distance(from: currentLocation!) 
     } 
    } 

    init(locationInfo:[String:Any]) { 
     self.id = locationInfo["id"] as! String 
     self.name = locationInfo["name"] as! String 
     self.type = locationInfo["type"] as! String 
     self.location = locationInfo["location"] as! String 
     self.image = locationInfo["image"] as! String 
     self.activity = locationInfo["activity"] as! String 
     self.latitude = locationInfo["latitude"] as! Double 
     self.longitude = locationInfo["longitude"] as! Double 
} 

    public var coordinate: CLLocationCoordinate2D { get { 
     let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude) 
     return coordinate 
     } 
    } 

} 

以下是有關部分從LocationTableViewController.swift:

class LocationTableViewController: UITableViewController { 

    var locations = [Location]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let locationService: LocationService = LocationService() 
     locations = locationService.readLocation() 
     locations.sort { $0.distance < $1.distance } 
     self.tableView.reloadData() 
    } 

    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(animated) 
     tableView.reloadData() 
    } 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let cellIdentifier = "cell" 
     let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! LocationTableViewCell 

     // Configure the cell 
     cell.nameLabel.text = locations[indexPath.row].name 
     cell.thumbnailImageView.image = UIImage(named: locations[indexPath.row].image) 
     cell.locationLabel.text = locations[indexPath.row].location 
     cell.typeLabel.text = locations[indexPath.row].type 
     return cell 
    } 
} 

這是我在AppDelegate中已經實現捕獲用戶的位置:

import UIKit 
import CoreLocation 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate { 

    var window: UIWindow? 
    var locationManager:CLLocationManager! 
    private var currentCoordinate:CLLocationCoordinate2D? 
    var currentLocation:CLLocation? 

    static var locations: Array<Location> = Array() 

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
     // Override point for customization after application launch. 

     // Location Manager 
     func setupLocationManager() { 
      locationManager = CLLocationManager() 
      locationManager?.delegate = self 
      self.locationManager?.requestAlwaysAuthorization() 
      locationManager?.desiredAccuracy = kCLLocationAccuracyBest 
      locationManager?.startUpdatingLocation() 
      } 
     } 

     return true 
    } 

    // Location Manager 
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
     if currentLocation == nil { 
      currentLocation = locations.last 
      locationManager?.stopMonitoringSignificantLocationChanges() 
      let locationValue:CLLocationCoordinate2D = manager.location!.coordinate 
      print("locations = \(locationValue)") 
      locationManager?.stopUpdatingLocation() 
     } 
    } 

回答

0

在位置模型中定義了一個名爲0的屬性。這是距用戶當前位置的距離。

var distance: Double { 
     get { 
      return CLLocation(latitude: latitude , longitude: longitude).distance(from: userLocation) 
     } 
    } 

這將返回距離CLLocationDistance,但你可以使用它作爲雙,因爲它使易於使用,同時排序。

現在你可以按照升序排列locations這樣並刷新數據

var locations = [Location]() 

locations.sort { $0.distance < $1.distance } 

self.tableView.reloadData() 
+0

謝謝!這可能是一個noob錯誤,但是我收到一個錯誤,說「使用未解析的標識符'userLocation'」。這是因爲我需要定義userLocation?我應該在哪裏做這件事? – joshlorschy

+0

@joshlorschy您必須在全局或某個共享實例中保存用戶位置。這取決於你 – Jaydeep

+0

我可以在Location.swift模型中做到嗎?或者它應該在AppDelegate中? – joshlorschy

相關問題