iOS - MKPlacemark 设置标题问题

标签 ios mkannotation clgeocoder

我在设置地标的 titlesubtitle 时遇到问题。

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";

给我一​​个错误:

Assigning to property with 'readonly' attribute not allowed

为什么这里不能设置Title和Subtitle?

最佳答案

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

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

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

我相信您现在已经找到了自己的解决方案,但我希望这在某种程度上能提供信息。

关于iOS - MKPlacemark 设置标题问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9608731/

相关文章:

ios - Apple Watch 应用程序 : run an MKDirectionsRequest through the parent iOS app in the background?

ios - 为什么在使用ofstream时我的NSString变量输出“1”?

iphone - 如何显示 MKAnnotation 的副标题 2 行文本并更改右侧按钮的图像?

ios - 切换到新场景时内存会增加

ios - 自定义字体在 IB 中设置时正确显示,但在以编程方式设置时不正确

ios - 将图钉放在 map 上时在注释中显示地址

ios7 - CLGeocoder 反向地理编码失败,错误域=NSURLErrorDomain Code=-1000 -

ios - reverseGeocodeLocation 每次都不起作用

iphone - MKAnnotationView 并将数据传递给另一个函数

ios - 将图像从 map 注释传递到详细信息 View Controller