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

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

addObject行上方代码中的注释提到了"MKPinAnnotation",但我认为它的意思是"MKPointAnnotation"。

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

替换此行:

[annotations addObject:item.placemark];

有了这些:

MKPlacemark *pm = item.placemark;
MKPointAnnotation *ann = [[MKPointAnnotation alloc] init];
ann.coordinate = pm.coordinate;
ann.title = pm.name;    //or whatever you want
//ann.subtitle = @"optional subtitle here";
[annotations addObject:ann];

相关内容

  • 没有找到相关文章

最新更新