首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mapkit :如何在MKLocalSearch结果地标上添加注释?

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

Stack Overflow用户
提问于 2013-04-16 02:07:03
回答 2查看 2.3K关注 0票数 1

基本上,我需要一种方法将注释放在搜索请求获得的所有“Walmart”上。我没有使用界面生成器,我只是使用这个应用程序的代码。

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

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-16 03:07:56

我建议在viewDidLoad中简单地创建mapview

代码语言:javascript
复制
- (void)viewDidLoad
{
    [super viewDidLoad];

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

    [self.view addSubview:map];
}

但当用户移动地图时,查找沃尔玛并添加注释:

代码语言:javascript
复制
- (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连接速度较慢的情况下浏览地图,则可能会获得更好的性能。)无论如何,该属性的定义如下:

代码语言:javascript
复制
@property (nonatomic, weak) MKLocalSearch *previousSearch;

还有其他可能的改进(例如,如果搜索正在进行,则使用UIActivityIndicatorView或网络活动指示器,可能会删除不在地图当前区域内的注释,等等),但希望您能理解。

票数 1
EN

Stack Overflow用户

发布于 2013-04-16 02:43:55

以下是方法:

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

https://stackoverflow.com/questions/16021870

复制
相关文章

相似问题

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