1

目前在请求“restaurant”类型的 mklocalsearch 请求(显示附近的餐馆)时使用苹果的 iOS 应用程序的地图视图,我得到的只是在设备上运行时美国的餐馆,而不是在当前位置(黎巴嫩)获取附近的餐馆.

这是我认为的代码确实在美国的日志餐厅加载了结果打印

_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);
        }
}];
4

1 回答 1

2

弄清楚了!我们必须手动设置地图视图的区域,否则默认情况下会假定该区域在美国。此代码是完整的,将显示您所在位置附近的餐馆

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

}

于 2014-05-27T20:21:54.597 回答