如何从最近到最远的位置对MKLocalSearchResponse进行排序

时间:2014-07-31 02:33:16

标签: ios objective-c mklocalsearch

我正在使用核心位置来获得我当前职位的CLLocation。然后,我使用MKLocalSearchRequest来查找靠近该位置的位置,以便搜索查询,例如“Coffee”或其他任何内容。

我从搜索中得到MKLocalSearchResponse,我想根据与CLLocation的距离按升序对结果进行排序。

这是获取结果的代码。

 // 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 个答案:

没有答案