从MKLocalSearch为注释创建标题

时间:2014-02-07 01:03:34

标签: ios mkmapview mkplacemark mklocalsearch

我添加了MKLocalSearch并且引脚正确显示。唯一的问题是引脚标题有名称和地址,我只想要名称。我该怎么改变呢这是我正在使用的代码 -

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{

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

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

1 个答案:

答案 0 :(得分:2)

由于title的{​​{1}}无法直接修改,因此您需要使用item.placemark中的值创建自定义注释或MKPointAnnotation

item.placemark行上方代码中的注释提及“MKPinAnnotation”,但我认为它的意思是“MKPointAnnotation”。)

下面的示例使用简单的选项,即使用SDK提供的预定义addObject类来创建您自己的简单注释。

替换此行:

MKPointAnnotation

用这些:

[annotations addObject:item.placemark];