MKLocalSearch搜索附近的餐馆,显示美国而不是当前位置



目前使用苹果的地图视图的iOS应用程序,当请求类型为"餐馆"的mklocalsearch请求(显示附近的餐馆)时,我得到的是美国的餐馆,而不是在当前位置(黎巴嫩)获得附近的餐馆。

这是我的视图的代码加载结果打印在日志餐馆的美国

_mapview.showsUserLocation=YES;
_mapview.delegate=self;
MKLocalSearchRequest *request =
[[MKLocalSearchRequest alloc] init];
request.naturalLanguageQuery=@"Restaurant";
request.region = _mapview.region;
MKLocalSearch *search =
[[MKLocalSearch alloc]initWithRequest:request];
[search startWithCompletionHandler:^(MKLocalSearchResponse
                                     *response, NSError *error) {
    if (response.mapItems.count == 0)
        NSLog(@"No Matches");
    else
        for (MKMapItem *item in response.mapItems)
        {
            NSLog(@"name = %@", item.name);
            NSLog(@"Phone = %@", item.phoneNumber);
        }
}];

明白了!我们必须手动设置地图视图的区域,否则它将默认假定该区域位于美国。这个代码是完整的,将显示附近的餐馆在你的位置

[super viewDidLoad];
[self.searchDisplayController setDelegate:self];
[self.ibSearchBar setDelegate:self];
self.ibMapView.delegate=self;
// Zoom the map to current location.
[self.ibMapView setShowsUserLocation:YES];
[self.ibMapView setUserInteractionEnabled:YES];
[self.ibMapView setUserTrackingMode:MKUserTrackingModeFollow];

CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate=self;
[locationManager startUpdatingLocation];
[self.ibMapView setRegion:MKCoordinateRegionMake(locationManager.location.coordinate, MKCoordinateSpanMake(0.2, 0.2))];
MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.region = self.ibMapView.region;
request.naturalLanguageQuery = @"restaurant";
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
localSearch = [[MKLocalSearch alloc] initWithRequest:request];
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error){
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    results = response;
    if (response.mapItems.count == 0)
        NSLog(@"No Matches");
    else
        for (MKMapItem *item in response.mapItems)
        {
            NSLog(@"name = %@", item.name);
            NSLog(@"Phone = %@", item.phoneNumber);
            [_matchingItems addObject:item];
            MKPointAnnotation *annotation =
            [[MKPointAnnotation alloc]init];
            annotation.coordinate = item.placemark.coordinate;
            annotation.title = item.name;
            [self.ibMapView addAnnotation:annotation];
        }
}];

}

相关内容

  • 没有找到相关文章

最新更新