【问题标题】:expand search region in MKlocalSearch mapkit在 MKlocalSearch mapkit 中展开搜索区域
【发布时间】:2014-04-12 04:58:54
【问题描述】:

我正在使用 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];

}

【问题讨论】:

    标签: ios objective-c mapkit mklocalsearch


    【解决方案1】:
    request.region = MKCoordinateRegionMakeWithDistance(self.mapView.userLocation.location.coordinate,
    120701, 120701);
    // 120701 meters is 75 miles
    

    或者如果您已经有位置:

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

    像这样独立设置搜索区域,可以在不扩大地图的情况下搜索大面积。

    【讨论】:

    • 有什么方法可以在全球范围内实现这一目标?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-18
    • 2020-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多