Ich kann die Liste der Standorte in einer Tabelle anzeigen. Aber jetzt möchte ich es basierend auf dem aktuellen Standort sortieren. DieseZeigt eine Liste der nächstgelegenen Standorte für den Benutzer in einer Tabelle an
ist, wie ich meine Positionen angezeigt:
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
}
habe ich versucht, diese Strecke in meiner Location
Klasse zu implementieren, die ich von https://stackoverflow.com/a/35200027/6362735 bekam aber ich bin nicht sicher, was ich tun müssen, um nächstes und wie man ordne es.
Hier ist meine Lage Klasse:
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)
}
}
den aktuellen Standort angezeigt:
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
}
Vielen Dank für Ihre Antwort. Aber könnten Sie näher erläutern, wo ich das in meiner App nennen würde? – mninety5
Es hängt davon ab, wie Ihre App strukturiert ist.Wenn Sie eine Schaltfläche oder etwas haben, das die Filteraktion auslöst, sollten Sie es dort ablegen. Wenn Sie möchten, dass Ihre Standorte immer nach Entfernung sortiert sind, können Sie sie beispielsweise in 'viewDidLoad' in den Controller Ihres TableViews einfügen. Ich werde die Antwort mit beiden Fällen aktualisieren. – jvrmed
Wie lege ich die Parameter fest? Wenn ich Ihre Sortierfunktion verwende, heißt es nur: 'Missing argument for parameter 'to' in call'. Es erwartet eine 'CLLocation'. – mninety5