ios - 在 MKlocalSearch mapkit 中扩展搜索区域

标签 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];

}

最佳答案

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

或者如果您已经有一个位置:

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

这样独立设置搜索区域,可以在不扩展 map 的情况下搜索大范围区域。

关于ios - 在 MKlocalSearch mapkit 中扩展搜索区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23026271/

相关文章:

ios - UIPickerView 完成按钮不起作用

ios - CLPlacemark位置也会提供地区信息,为什么?

objective-c - 使用MD5算法时出错

iphone - MPMoviePlayerController 全屏旋转,而父 View Controller 仅支持纵向

ios - 使用 OpenAL 对音频电平进行采样

ios - shadowRadius 不能设置的更高

ios - 如何放大用户当前位置?

swift - 为 MapKit 中的每个注解添加 Id

ios - 如何使用 XCUIKeyboardKey 常量?

objective-c - iOS 和 OpenCV : debug malloc_error_break on device