How do I sort a MKLocalSearchResponse from closest to farthest location

296 Views Asked by At

I am using core location to get a CLLocation of my current position. I then use MKLocalSearchRequest in order to find places close to that location that mach a search query, such as "Coffee" or whatever.

I get a MKLocalSearchResponse back from the search and I would like to sort the result in ascending order based on distance from the CLLocation.

Here is my code that gets the result.

 // Cancel any previous searches.
    [localSearch cancel];

    [locationManager stopUpdatingLocation];

    // Perform a new search.
    MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
    request.naturalLanguageQuery = _lastSearchTerm;
    request.region = aRegion;

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    localSearch = [[MKLocalSearch alloc] initWithRequest:request];

    [localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error){

        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

        if (error != nil) {
            [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Map Error",nil)
                                        message:[error localizedDescription]
                                       delegate:nil
                              cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:nil] show];
            return;
        }

        if ([response.mapItems count] == 0) {
            [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"No Results",nil)
                                        message:nil
                                       delegate:nil
                              cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:nil] show];
            return;
        }

        results = response;

        [locationManager stopUpdatingLocation];

        [_tableView reloadData];
0

There are 0 best solutions below