테이블의 위치 목록을 표시 할 수 있습니다. 하지만 지금은 현재 위치에 따라 정렬하고 싶습니다.테이블에 사용자에게 가장 가까운 위치 목록 표시
이것은 내 위치를 표시하는 방법입니다 : 내가 https://stackoverflow.com/a/35200027/6362735에서 가져온 내 Location
클래스에서이 거리를 구현하기 위해 노력했다
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "locationCell", for: indexPath)
let location = LocationManager.shared.locations[indexPath.row]
cell.textLabel?.text = location.name
return cell
}
을하지만 난 옆에 어떻게 무엇을해야하는지 잘 모르겠어요 정렬 해. 현재 위치를 표시
class Location {
var name: String
var latitude: Double
var longitude: Double
var location:CLLocation {
return CLLocation(latitude: latitude, longitude: longitude)
}
init?(json: JSON) {
guard let name = json["name"] as? String, let latitude = json["latitude"] as? Double, let longitude = json["longitude"] as? Double else { return nil }
self.name = name
self.latitude = latitude
self.longitude = longitude
}
func distance(to location: CLLocation) -> CLLocationDistance {
return location.distance(from: self.location)
}
}
:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
mapView.setRegion(region, animated: true)
self.mapView.showsUserLocation = true
}
답장을 보내 주셔서 감사합니다. 하지만 내 앱에서 이걸 어디에 부르는지 정교 할 수 있니? – mninety5
앱 구성 방식에 따라 다릅니다.필터링 동작을 트리거하는 버튼이나 무언가가 있다면, 거기에 넣어야합니다. 당신의 위치가 항상 거리별로 정렬되기를 원하면, 예를 들어 viewDidLoad에서 TableView의 컨트롤러 안에 넣을 수 있습니다. 두 가지 경우에 대한 답변을 업데이트하겠습니다. – jvrmed
내가 매개 변수에 무엇을 넣을 까? 내가 당신의 정렬 기능을 사용할 때, 단지 '매개 변수에 대한 누락 된 인수'가 '호출 중'이라고 말합니다. 그것은'CLLocation'을 기대하고 있습니다. – mninety5