您好我需要帮助才能选择并仅显示距用户当前位置5公里的水果。我必须在表视图单元格中显示它们。这是我的代码:
@IBOutlet weak var LabelTest: UILabel!
var locationManager = CLLocationManager()
var lat = Double()
var long = Double()
var latitude = Double()
var longitude = Double()
struct Fruit {
let name : String
let location : CLLocation
let latitude : Double
let longitude: Double
let imageURL : NSURL
let description : String
func distanceTo(location:CLLocation) -> Int
{
let distanceMeters = location.distanceFromLocation(self.location)
let distanceKilometers = distanceMeters / 1000.00
return Int(round(100 * distanceKilometers) / 100)
}
}
var fruits = [Fruit]()
func parseFruits() {
if CalculateDistance() < 5 {
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 = aFruit["Latitude"] as! Double
let longitude = aFruit["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,latitude:latitude,longitude: longitude, imageURL: NSURL(string:imageURL)!, description: description)
fruits.append(fruit)
}
self.tableView.reloadData()
}
}
override func viewDidLoad() {
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
}
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()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location:CLLocationCoordinate2D = manager.location!.coordinate
lat = location.latitude
long = location.longitude
}
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))
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("Error")
}
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
}
在TableView单元格中,我只需要显示离用户位置5公里的水果。我试着做点什么,但似乎它没有用。任何想法如何解决它?
答案 0 :(得分:1)
就这样做:
// Update your CalculateDistance method to the following
func CalculateDistance(lat: Double, long: Double) -> 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)
}
// When you parse your fruits, you can make the check if it´s within 5km then you can add it to your array or add it to a separate array
if (CalculateDistance(latitude, longitude) <= 5000{
fruits.append(fruit)
}
答案 1 :(得分:0)
您的CalculateDistance方法有何功能?你应该过滤你的阵列。
func shouldShowFruit(fruit: Fruit) -> Bool{
let userLocation = CLLocation(latitude: lat, longitude: long)
let destinationLocation = CLLocation(latitude:fruit.latitude, longitude: fruit.longitude)
let distance = userLocation.distanceFromLocation(destinationLocation)
return distance < 5000
}
为数组中的每个项调用此方法,如果返回false则从数组中删除。