I have a table view with locations, and I want to filter them by nearby locations when I press a button. I was thinking that when I press the button, the app can reload the table view with the nearby locations, but I can't figure out how to do the filtering.
Asked
Active
Viewed 176 times
-1
-
You want to exclude "filter" locations if they are not nearby? Or do you just want to show them sorted from nearest to furthest? The latest can be done `NSArray`'s method `sortedArrayUsingComparator:` – Mohannad A. Hassan Jun 05 '13 at 22:43
-
sorry I express my self wrong I want to sort them by nearest location – darkjuso Jun 05 '13 at 22:56
1 Answers
1
Suppose you have an NSArray of CLLocation objects, let's call it locations and CLLocation *currenLocation that represent your current location.
NSArray *sortedLocations = [locations sortedArrayUsingComparator:^(id obj1, id obj2) {
CLLocationDistance distance1 = [currentLocation distanceFromLocation: (CLLocation *)obj1];
CLLocationDistance distance2 = [currentLocation distanceFromLocation: (CLLocation *)obj2];
return distance1 - distance2;
}];
Edit: You can use an object of CLLocationManager to get frequent updates of user's location via delegate methods. You can start it by startUpdatingLocation. Check this to give you more information.
You can create a single CLLocation if you have the geographical location, by – initWithLatitude:longitude:.
Community
- 1
- 1
Mohannad A. Hassan
- 1,660
- 1
- 14
- 24
-
-
-
yes please I used CLLocationCoordinate2D on am map app I did before but I don't know who to do it for this one – darkjuso Jun 06 '13 at 22:06
-