首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在MKlocalSearch mapkit中展开搜索区域

在MKlocalSearch mapkit中展开搜索区域
EN

Stack Overflow用户
提问于 2014-04-12 04:58:54
回答 1查看 1.6K关注 0票数 0

我正在使用mapkit和MKLocalSearch搜索用户当前区域内的业务位置。然而,这个区域似乎并不像我所需要的那么大,而且一些业务地点不会出现在我的搜索中,除非我将我目前的位置设置在更接近该业务的位置。我想要的跨度是75英里,有什么建议如何扩大区域在我的代码?我刚开始使用mapkit,一直找不到任何有用的东西。这是我的搜索方法。

代码语言:javascript
复制
- (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];

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-27 21:18:31

代码语言:javascript
复制
request.region = MKCoordinateRegionMakeWithDistance(self.mapView.userLocation.location.coordinate,
120701, 120701);
// 120701 meters is 75 miles

或者如果你已经有了一个地点:

代码语言:javascript
复制
request.region = MKCoordinateRegionMakeWithDistance(location.coordinate,
120701, 120701);

像这样独立地设置搜索区域可以让您搜索大范围的区域,而不需要展开地图。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23026271

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档