首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为来自MKLocalSearch的注记创建标题

为来自MKLocalSearch的注记创建标题
EN

Stack Overflow用户
提问于 2014-02-07 09:03:34
回答 1查看 343关注 0票数 1

我已经添加了一个MKLocalSearch,并且引脚显示正确。唯一的问题是引脚标题有名字和地址,因为我只想要名字。我该如何改变这一点。这是我使用的代码-

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

回答 1

Stack Overflow用户

发布于 2014-02-07 10:48:12

由于item.placemarktitle不能直接修改,因此您需要使用来自item.placemark的值创建一个自定义注释或MKPointAnnotation

( addObject行上面的代码中的注释提到了"MKPinAnnotation“,但我认为它的本意是”MKPointAnnotation“。)

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

替换此行:

代码语言:javascript
复制
[annotations addObject:item.placemark];

有了这些:

代码语言:javascript
复制
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];
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21617414

复制
相关文章

相似问题

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