Mapkit:如何在MKLocalSearch结果地标上添加注释?

时间:2013-04-15 18:07:03

标签: iphone ios mapkit objective-c-blocks mkannotation

基本上我需要一种方法来对搜索请求选中的所有“Walmarts”进行注释。我没有使用界面构建器,我只是使用此应用程序的代码。

MKMapView * map = [[MKMapView alloc] initWithFrame:
                   CGRectMake(0, 0, 320, 480)];
map.delegate = self;
[self.view addSubview:map];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.



MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.naturalLanguageQuery = @"Walmart";
request.region = map.region;

2 个答案:

答案 0 :(得分:1)

我建议您只在viewDidLoad中创建mapview:

- (void)viewDidLoad
{
    [super viewDidLoad];

    MKMapView * map = [[MKMapView alloc] initWithFrame:self.view.bounds];
    map.delegate = self;

    [self.view addSubview:map];
}

但是当用户移动地图时,请查找Walmarts并添加注释:

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
    // if on slow network, it's useful to keep track of the previous
    // search, and cancel it if it still exists

    [self.previousSearch cancel];

    // create new search request

    MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
    request.naturalLanguageQuery = @"Walmart";
    request.region = mapView.region;

    // initiate new search

    MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:request];
    [localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {

        NSMutableArray *annotations = [NSMutableArray array];

        [response.mapItems enumerateObjectsUsingBlock:^(MKMapItem *item, NSUInteger idx, BOOL *stop) {

            // if we already have an annotation for this MKMapItem,
            // just return because you don't have to add it again

            for (id<MKAnnotation>annotation in mapView.annotations)
            {
                if (annotation.coordinate.latitude == item.placemark.coordinate.latitude &&
                    annotation.coordinate.longitude == item.placemark.coordinate.longitude)
                {
                    return;
                }
            }

            // otherwise, add it to our list of new annotations
            // ideally, I'd suggest a custom annotation or MKPinAnnotation, but I want to keep this example simple

            [annotations addObject:item.placemark];
        }];

        [mapView addAnnotations:annotations];
    }];

    // update our "previous" search, so we can cancel it if necessary

    self.previousSearch = localSearch;
}

显然,此代码示例假定您具有上一次搜索操作的weak属性。 (严格来说,这不是必要的,但如果您在Internet连接速度较慢时浏览地图,可能会给您带来更好的性能。)无论如何,该属性将定义如下:

@property (nonatomic, weak) MKLocalSearch *previousSearch;

还有其他可能的改进(例如UIActivityIndicatorView或网络活动指示符,如果搜索正在进行中,可能会删除不在地图当前区域内的注释等),但希望您明白这一点。

答案 1 :(得分:0)

这是方法:

MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:request];
[self.localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {

if(error)
{
        NSLog(@"localSearch startWithCompletionHandlerFailed!  Error: %@", error);
        return;
} 
else 
{
        for(MKMapItem *mapItem in response.mapItems)
        {
             MKPinAnnotation *annotation = [[MKPinAnnotation alloc] initWithCoordinate: mapItem.placemark.location.coordinate];
             [map addAnnotation:annotation];
             NSLog(@"Name for result: = %@", mapItem.name);
        }
}}];