【发布时间】:2016-07-08 09:25:29
【问题描述】:
我用地图视图和表格视图构建了一个视图混合,我希望我可以按照与用户位置的距离对单元格进行排序,我尝试使用 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({ $0.coordinate.distanceFromLoaction(self.coordinate) < $1.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
}
感谢您的建议。
【问题讨论】:
-
distanceFromLocation()是CLLocation方法而不是CLLocationCoordinate2D方法。所以你必须把CLLocationCoordinate2D对象转换成CLLocation(很简单)然后调用distanceFromLocation()就可以了。 -
嗨@Larme你能教我如何将 CLLocationCoordinate2d 快速转换为 CLLocation 吗?我在整个 stackoverflow 上进行了搜索,但仍然可以得到它。你。
-
var clLocationObject = CLLocation(latitude: cllocationCoordinate2D.latitude, longitude: cllocationCoordinate2D.longitude)或类似的东西。 -
抱歉进一步询问,但它显示错误,实例成员“纬度”不能用于类型“CLLocationCoordinate2D”
标签: swift sorting mapkit distance cllocationcoordinate2d