2016-08-26 11 views
2

私はすぐにアプリを開発しています。ユーザーの現在の場所とJSONファイルから取得した他の場所を比較する必要があります。次に、ユーザーの場所から特定の範囲にあるすべての場所を表示する必要があります。この範囲は私がUISliderから取るものです。ユーザーがスライダーで25kmを選択すると、アプリは現在のユーザーの場所を特定し、この範囲内にあるフルーツをすべて表示する必要があります。user'r現在の場所を他の場所と比較してUITable Viewで表示するにはどうすればいいですか?

import UIKit 
    import SwiftyJSON 
    import MapKit 
    import CoreLocation 


class TableViewController: UITableViewController,CLLocationManagerDelegate { 
@IBOutlet weak var Bar: UIToolbar! 


@IBOutlet weak var LabelTest: UILabel! // this is the slider value, I segue it from the previous viewcontroller 
var manager = CLLocationManager() 

struct Fruit { 
    let name : String 
    let location : CLLocation 
    let imageURL : NSURL 
    let description : String 
} 
var fruits = [Fruit]() 


func parseFruits() { 
    guard let url = NSBundle.mainBundle().URLForResource("cities", withExtension: "json"), jsonData = NSData(contentsOfURL: url) else { 
     print("Error finding JSON File") 
     return 
    } 

    let jsonObject = JSON(data: jsonData) 

    let fruitArray = jsonObject["fruits"].arrayValue 
    for aFruit in fruitArray { 
     let name = aFruit["Name"].stringValue 
     let latitude = city["Latitude"] as! Double 
     let longitude = city["Longitude"] as! Double 
     let location = CLLocation(latitude: latitude, longitude: longitude) 
     let imageURL = aFruit["Picture"].stringValue 
    let description = aFruit["Description"].stringValue 

     let fruit = Fruit(name: name,location: location,imageURL: NSURL(string:imageURL)!, description: description) 
     fruits.append(fruit) 
    } 

    self.tableView.reloadData() 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 
    parseFruits() 

    // Uncomment the following line to preserve selection between presentations 
    // self.clearsSelectionOnViewWillAppear = false 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem() 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

// MARK: - Table view data source 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return 1 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return fruits.count 
    } 



override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell: TableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! TableViewCell 
    let fruit = fruits[indexPath.row] 
    cell.CellTitle.text = fruit.name 
    cell.CellDescription.text = fruit.description 


    let image = UIImage(data: NSData(contentsOfURL:(string: fruit.imageURL))!) 
    cell.CellImage.image = image 

    return cell 
      } 

現在、私は、ユーザーの現在位置を計算していないと私はそれを比較するわけではない誰かがそれを行う方法私を見ることができれば、私は喜んでいるだろう。

func CalculateDistance() { 
    let userLocation = CLLocation(latitude: lat, longitude: long) 
    let destinationLocation = CLLocation(latitude:latitude, longitude: longitude)// latitude and longitude from the json file 
    let distance = userLocation.distanceFromLocation(destinationLocation) 
} 

答えて

1

あなたは次の操作を行うことができます2つの位置の間の距離を計算したいときは:編集した

let userLocation = CLLocation(latitude: lat, longitude: long) 
let destinationLocation = CLLocation(latitude: (dest.lat as NSString).doubleValue, longitude: (dest.long as NSString).doubleValue) 
let distance = userLocation.distanceFromLocation(destinationLocation) 

は、ユーザーの現在位置ですuserLocationを取得します。次に、目的地の場所を持っていて、distanceFromLocation関数の助けを借りてCoreLocationの一部である距離を計算します。

その後、私は最も近い5メートルまでの距離を丸める方法行っている:あなたは、もちろん10にこれを変更することができます

var distanceToFive = roundToFive(distance) 

private func roundToFive(x : Double) -> Int { 
    return 5 * Int(round(x/5.0)) 
} 

を、20など

編集:
とします現在の場所を取得します。
CLLocationManagerDelegateをクラス継承に追加します。 var locationManager = CLLocationManager()とlatとlongの2つの変数を宣言します。 viewDidLoad

self.locationManager.requestWhenInUseAuthorization() 
    if CLLocationManager.locationServicesEnabled() { 
     locationManager.delegate = self 
     locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    } 

を行い、その後、ユーザーのための場所は、次のメソッド宣言を取得する:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
     let location:CLLocationCoordinate2D = manager.location!.coordinate 
     lat = location.latitude 
     long = location.longitude 
    } 

    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) { 
     print("Error") 
    } 

EDIT2:

func CalculateDistance() -> Int{ 
    let userLocation = CLLocation(latitude: lat, longitude: long) 
    let destinationLocation = CLLocation(latitude:latitude, longitude: longitude)// latitude and longitude from the json file 
    let distance = userLocation.distanceFromLocation(destinationLocation) 

    return roundToFive(distance) 
} 

private func roundToFive(x : Double) -> Int { 
    return 5 * Int(round(x/5.0)) 
} 
+0

は私だけではないことができ、高速応答をありがとうあなたの現在の緯度と経度をどのように取得するのですか? – Dakata

+0

let userLocation = CLLocation(latitude:lat、longitude:long) – Dakata

+0

@Dakataはupdをチェックしますate –

関連する問題