tableview 按距离排序 swift
tableview sort by distance swift
我构建了一个混合了地图视图和table视图的视图,我希望我可以根据与用户位置的距离对单元格进行排序,我尝试使用 distanceFromLoaction 方法,但它显示 CLLocationCoordinate 没有会员 distanceFromLocaiton,有没有其他方法可以做到或修复它?
这是我的数组结构
import UIKit
import MapKit
class tripSpot: NSObject, MKAnnotation{
var title: String?
var coordinate: CLLocationCoordinate2D
var regionRadius: Double
var location: String?
var type: String?
init(title:String , coordinate: CLLocationCoordinate2D , regionRadius: Double, location: String, type: String ){
self.title = title
self.coordinate = coordinate
self.regionRadius = regionRadius
self.location = location
self.type = type
}
}
和我的 tableviewcode
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MapCell") as! mapTableViewCell
self.tripspot.sort({ [=12=].coordinate.distanceFromLoaction(self.coordinate) < .coordinate.distanceInKilometersTo(self.coordinate)})
if searchController.active{
cell.title.text = searchResults[indexPath.row].title
cell.location.text = searchResults[indexPath.row].location
return cell
}else{
cell.title.text = tripspot[indexPath.row].title
cell.location.text = tripspot[indexPath.row].location
return cell
}
感谢任何建议。
import UIKit
import CoreLocation
final class Places {
var title: String?
var cllocation: CLLocation
var regionRadius: Double
var location: String?
var type: String?
var distance : Double?
var coordinate : CLLocationCoordinate2D
init(title:String , cllocation: CLLocation , regionRadius: Double, location: String, type: String ,distance:Double!,coordinate: CLLocationCoordinate2D){
self.title = title
self.cllocation = cllocation
self.coordinate = coordinate
self.regionRadius = regionRadius
self.location = location
self.type = type
self.distance = distance
}
// Function to calculate the distance from given location.
func calculateDistance(fromLocation: CLLocation?) {
distance = cllocation.distanceFromLocation(fromLocation!)
}
}
let fromLocation:CLLocation = CLLocation(latitude: 24.186965, longitude: 120.633268)
var places:[Places] = [
Places( title: "Title1", cllocation: CLLocation( latitude :24.181143, longitude: 120.593158), regionRadius: 300.0, location: "LocationTitle1", type: "Food",distance : CLLocation( latitude :24.181143, longitude: 120.593158).distanceFromLocation(fromLocation),coordinate : CLLocationCoordinate2DMake(24.181143,120.593158)),
Places( title: "Title2", cllocation: CLLocation(latitude:24.14289,longitude:120.679901), regionRadius:150.0, location:"LocationTitle2",type: "Food",distance : CLLocation(latitude:24.14289,longitude:120.679901).distanceFromLocation(fromLocation),coordinate : CLLocationCoordinate2DMake(24.14289,120.679901)),
Places( title: "Title3", cllocation: CLLocation(latitude : 24.180407, longitude:120.645086), regionRadius: 300.0, location:"LocationTitle3", type: "Food",distance : CLLocation(latitude : 24.180407, longitude:120.645086).distanceFromLocation(fromLocation),coordinate : CLLocationCoordinate2DMake(24.180407,120.645086)),
Places( title: "Title4", cllocation: CLLocation(latitude: 24.149062,longitude:120.684891), regionRadius: 300.0, location: "LocationTitle4", type: "Food",distance : CLLocation(latitude: 24.149062,longitude:120.684891).distanceFromLocation(fromLocation),coordinate : CLLocationCoordinate2DMake(24.149062,120.684891)),
Places( title: "Title5", cllocation: CLLocation(latitude:24.138598,longitude:120.672096 ), regionRadius:150.0, location:"LocationTitle5",type: "Food",distance : CLLocation(latitude:24.138598,longitude:120.672096 ).distanceFromLocation(fromLocation),coordinate : CLLocationCoordinate2DMake(24.138598,120.672096)),
Places( title: "Title6", cllocation: CLLocation(latitude :24.1333434,longitude:120.680744), regionRadius:100.0, location:"LocationtTitle6",type: "Culture",distance : CLLocation(latitude :24.1333434,longitude:120.680744).distanceFromLocation(fromLocation),coordinate : CLLocationCoordinate2DMake(24.1333434,120.680744))
]
for k in 0...(places.count-1) {
print("\(places[k].distance)")
}
for place in places {
place.calculateDistance(fromLocation) // Replace YOUR_LOCATION with the location you want to calculate the distance to.
}
places.sortInPlace({ [=10=].distance < .distance })
for n in 0...(places.count-1) {
print("\(places[n].distance)")
}
输出:
//before sort array
Optional(4126.1395817058)
Optional(6803.61030342841)
Optional(1403.39181021788)
Optional(6718.92222011204)
Optional(6653.47447563344)
Optional(7651.92757760459)
//after sort array
Optional(1403.39181021788)
Optional(4126.1395817058)
Optional(6653.47447563344)
Optional(6718.92222011204)
Optional(6803.61030342841)
Optional(7651.92757760459)
我构建了一个混合了地图视图和table视图的视图,我希望我可以根据与用户位置的距离对单元格进行排序,我尝试使用 distanceFromLoaction 方法,但它显示 CLLocationCoordinate 没有会员 distanceFromLocaiton,有没有其他方法可以做到或修复它?
这是我的数组结构
import UIKit
import MapKit
class tripSpot: NSObject, MKAnnotation{
var title: String?
var coordinate: CLLocationCoordinate2D
var regionRadius: Double
var location: String?
var type: String?
init(title:String , coordinate: CLLocationCoordinate2D , regionRadius: Double, location: String, type: String ){
self.title = title
self.coordinate = coordinate
self.regionRadius = regionRadius
self.location = location
self.type = type
}
}
和我的 tableviewcode
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MapCell") as! mapTableViewCell
self.tripspot.sort({ [=12=].coordinate.distanceFromLoaction(self.coordinate) < .coordinate.distanceInKilometersTo(self.coordinate)})
if searchController.active{
cell.title.text = searchResults[indexPath.row].title
cell.location.text = searchResults[indexPath.row].location
return cell
}else{
cell.title.text = tripspot[indexPath.row].title
cell.location.text = tripspot[indexPath.row].location
return cell
}
感谢任何建议。
import UIKit
import CoreLocation
final class Places {
var title: String?
var cllocation: CLLocation
var regionRadius: Double
var location: String?
var type: String?
var distance : Double?
var coordinate : CLLocationCoordinate2D
init(title:String , cllocation: CLLocation , regionRadius: Double, location: String, type: String ,distance:Double!,coordinate: CLLocationCoordinate2D){
self.title = title
self.cllocation = cllocation
self.coordinate = coordinate
self.regionRadius = regionRadius
self.location = location
self.type = type
self.distance = distance
}
// Function to calculate the distance from given location.
func calculateDistance(fromLocation: CLLocation?) {
distance = cllocation.distanceFromLocation(fromLocation!)
}
}
let fromLocation:CLLocation = CLLocation(latitude: 24.186965, longitude: 120.633268)
var places:[Places] = [
Places( title: "Title1", cllocation: CLLocation( latitude :24.181143, longitude: 120.593158), regionRadius: 300.0, location: "LocationTitle1", type: "Food",distance : CLLocation( latitude :24.181143, longitude: 120.593158).distanceFromLocation(fromLocation),coordinate : CLLocationCoordinate2DMake(24.181143,120.593158)),
Places( title: "Title2", cllocation: CLLocation(latitude:24.14289,longitude:120.679901), regionRadius:150.0, location:"LocationTitle2",type: "Food",distance : CLLocation(latitude:24.14289,longitude:120.679901).distanceFromLocation(fromLocation),coordinate : CLLocationCoordinate2DMake(24.14289,120.679901)),
Places( title: "Title3", cllocation: CLLocation(latitude : 24.180407, longitude:120.645086), regionRadius: 300.0, location:"LocationTitle3", type: "Food",distance : CLLocation(latitude : 24.180407, longitude:120.645086).distanceFromLocation(fromLocation),coordinate : CLLocationCoordinate2DMake(24.180407,120.645086)),
Places( title: "Title4", cllocation: CLLocation(latitude: 24.149062,longitude:120.684891), regionRadius: 300.0, location: "LocationTitle4", type: "Food",distance : CLLocation(latitude: 24.149062,longitude:120.684891).distanceFromLocation(fromLocation),coordinate : CLLocationCoordinate2DMake(24.149062,120.684891)),
Places( title: "Title5", cllocation: CLLocation(latitude:24.138598,longitude:120.672096 ), regionRadius:150.0, location:"LocationTitle5",type: "Food",distance : CLLocation(latitude:24.138598,longitude:120.672096 ).distanceFromLocation(fromLocation),coordinate : CLLocationCoordinate2DMake(24.138598,120.672096)),
Places( title: "Title6", cllocation: CLLocation(latitude :24.1333434,longitude:120.680744), regionRadius:100.0, location:"LocationtTitle6",type: "Culture",distance : CLLocation(latitude :24.1333434,longitude:120.680744).distanceFromLocation(fromLocation),coordinate : CLLocationCoordinate2DMake(24.1333434,120.680744))
]
for k in 0...(places.count-1) {
print("\(places[k].distance)")
}
for place in places {
place.calculateDistance(fromLocation) // Replace YOUR_LOCATION with the location you want to calculate the distance to.
}
places.sortInPlace({ [=10=].distance < .distance })
for n in 0...(places.count-1) {
print("\(places[n].distance)")
}
输出:
//before sort array
Optional(4126.1395817058)
Optional(6803.61030342841)
Optional(1403.39181021788)
Optional(6718.92222011204)
Optional(6653.47447563344)
Optional(7651.92757760459)
//after sort array
Optional(1403.39181021788)
Optional(4126.1395817058)
Optional(6653.47447563344)
Optional(6718.92222011204)
Optional(6803.61030342841)
Optional(7651.92757760459)