我正在使用swift开发应用程序,我必须将用户的当前位置与从JSON文件中获取的其他位置进行比较。然后我必须显示从用户位置开始的某个范围内的所有位置。这个范围我从一个UISlider。我的意思是当用户在滑块中选择25km时,应用程序必须确定当前用户的位置并显示此范围内的所有水果。
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)
}
答案 0 :(得分:1)
如果要计算两个位置之间的距离,可以执行以下操作:
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
函数是var distanceToFive = roundToFive(distance)
private func roundToFive(x : Double) -> Int {
return 5 * Int(round(x / 5.0))
}
的一部分。
然后我做了一个方法,将距离四舍五入到最近的5米:
CLLocationManagerDelegate
您当然可以将此更改为10,20等。
修改:
强>
并获得当前位置:
将var locationManager = CLLocationManager()
添加到类继承中。声明viewDidLoad
和两个变量一个用于lat,一个用于long。在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")
}
然后获取用户的位置声明以下方法:
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))
}
EDIT2:
data.frame