首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS - MKPlacemark集标题问题

iOS - MKPlacemark集标题问题
EN

Stack Overflow用户
提问于 2012-03-07 20:42:30
回答 1查看 4K关注 0票数 2

我在设置地名的标题和副标题上有问题。

代码语言:javascript
复制
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
            [geocoder geocodeAddressString:location 
                 completionHandler:^(NSArray* placemarks, NSError* error){
                     if (placemarks && placemarks.count > 0) {
                         CLPlacemark *topResult = [placemarks objectAtIndex:0];
                         MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];

                         placemark.title = @"Some Title";
                         placemark.subtitle = @"Some subtitle";

                         MKCoordinateRegion region = self.mapView.region;
                         region.center = placemark.region.center;
                         region.span.longitudeDelta /= 8.0;
                         region.span.latitudeDelta /= 8.0;

                         [self.mapView setRegion:region animated:YES];
                         [self.mapView addAnnotation:placemark];
                     }
                 }
             ];

placemark.title = @"Some Title";placemark.subtitle = @"Some subtitle";

给我一个错误:

代码语言:javascript
复制
Assigning to property with 'readonly' attribute not allowed

为什么我不能在这里设置标题和副标题?

EN

回答 1

Stack Overflow用户

发布于 2012-07-06 20:05:30

我想我会唤醒这条线给你一个我想出的解决方案。

据我所知,MKPlacemark的标题/字幕是由于固有的赋值而具有的只读属性。但是,使用我找到的解决方案,您可以简单地将MKPlacemark传递到MKPointAnnotation中,如下所示:

代码语言:javascript
复制
CLPlacemark *topResult = [placemarks objectAtIndex:0];

// Create an MLPlacemark

MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];

// Create an editable PointAnnotation, using placemark's coordinates, and set your own title/subtitle
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = placemark.coordinate;
point.title = @"Sample Location";
point.subtitle = @"Sample Subtitle";


// Set your region using placemark (not point)          
MKCoordinateRegion region = self.mapView.region;
region.center = placemark.region.center;
region.span.longitudeDelta /= 8.0;
region.span.latitudeDelta /= 8.0;

// Add point (not placemark) to the mapView                                              
[self.mapView setRegion:region animated:YES];
[self.mapView addAnnotation:point];

// Select the PointAnnotation programatically
[self.mapView selectAnnotation:point animated:NO];

请注意,最后的[self.mapView selectAnnotation:point animated:NO];是一个解决办法,以允许自动弹出的广场.然而,animated:BOOL部分似乎只适用于iOS5中的NO --如果您遇到手动弹出点注释的问题,您可能希望实现一个解决方案,在这里可以找到:MKAnnotation not getting selected in iOS5

我相信你已经找到了自己的解决方案,但我希望这在某种程度上是信息丰富的。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9608731

复制
相关文章

相似问题

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