首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >附近餐厅的MKLocalSearch显示美国餐厅,而不是现在的位置

附近餐厅的MKLocalSearch显示美国餐厅,而不是现在的位置
EN

Stack Overflow用户
提问于 2014-05-25 23:14:08
回答 1查看 1.4K关注 0票数 1

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

这是我认为的代码,在美国的餐馆日志上加载了结果打印。

代码语言:javascript
复制
_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);
        }
}];
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-27 20:21:54

弄明白了!我们必须手动设置地图视图的区域,否则它将假定该区域默认在美国。此代码已完成,并将在您所在的位置显示附近的餐馆。

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

}

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

https://stackoverflow.com/questions/23860836

复制
相关文章

相似问题

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