2017-08-11 12 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

ロケーションモデルにもう1つのプロパティが定義されています。。ユーザーの現在地からの距離です。

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

関連する問題