在MKlocalSearch mapkit中扩展搜索区域

时间:2014-04-12 04:58:54

标签: ios objective-c mapkit mklocalsearch

我正在使用mapkit和MKLocalSearch搜索用户当前区域内的商户位置。然而,该区域似乎并不像我需要的那么大,并且除非我将当前位置设置为更接近该业务,否则某些业务位置将不会显示在我的搜索中。我希望跨度为75英里,有关如何在我的代码中扩展该区域的任何建议?我是使用mapkit的新手,无法找到任何有用的东西。这是我的搜索方法。

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {

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

// Perform a new search.
MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.naturalLanguageQuery = searchBar.text;
request.region = self.mapView.region;

[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;

    [self.searchDisplayController.searchResultsTableView reloadData];
}];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [results.mapItems count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath {

static NSString *IDENTIFIER = @"SearchResultsCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:IDENTIFIER];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:IDENTIFIER];
}

MKMapItem *item = results.mapItems[indexPath.row];

cell.textLabel.text = item.name;
cell.detailTextLabel.text = item.placemark.addressDictionary[@"Street"];

return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.searchDisplayController setActive:NO animated:YES];

MKMapItem *item = results.mapItems[indexPath.row];


[self.mapView setCenterCoordinate:item.placemark.location.coordinate animated:YES];

[self.mapView setUserTrackingMode:MKUserTrackingModeNone];

}

1 个答案:

答案 0 :(得分:2)

request.region = MKCoordinateRegionMakeWithDistance(self.mapView.userLocation.location.coordinate,
120701, 120701);
// 120701 meters is 75 miles

或者如果您已经有位置:

request.region = MKCoordinateRegionMakeWithDistance(location.coordinate,
120701, 120701);

像这样独立设置搜索区域允许您在不扩展地图的情况下搜索大区域。