从MKLocalSearch创建注释标题 [英] Create a title for annotation from MKLocalSearch

查看:111
本文介绍了从MKLocalSearch创建注释标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了一个MKLocalSearch,并且图钉正确显示.唯一的问题是,如果我只想要名称,则引脚标题同时具有名称和地址.我将如何改变这一点.这是我正在使用的代码-

I have added an MKLocalSearch and the pins are displaying correctly. The only problem is that the pins titles have both the name and address, were I only want the name. How would I change this. Here is the code I am using -

- (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.

Since the title of the item.placemark cannot be directly modified, you'll need to create a custom annotation or MKPointAnnotation using the values from item.placemark.

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

(The comment in the code above the addObject line mentions an "MKPinAnnotation" but I think it was meant to say "MKPointAnnotation".)

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

The example below uses the simple option of using the pre-defined MKPointAnnotation class provided by the SDK for creating your own, simple annotations.

替换此行:

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

这篇关于从MKLocalSearch创建注释标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆