iOS - MKPlacemark设置标题问题

Rom*_*mes 2 mkannotation ios clgeocoder

我在设置地标的标题副标题时遇到问题.

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];
                     }
                 }
             ];
Run Code Online (Sandbox Code Playgroud)

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

给我一个错误:

Assigning to property with 'readonly' attribute not allowed
Run Code Online (Sandbox Code Playgroud)

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

Sar*_*eph 7

以为我会唤醒这个帖子并给你一个我想出的解决方案.

据我所知,由于固有的分配,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];
Run Code Online (Sandbox Code Playgroud)

请注意,最终[self.mapView selectAnnotation:point animated:NO];的解决方法是允许自动弹出地标.但是,该animated:BOOL部分似乎只适用NO于iOS5 - 如果您遇到手动弹出点注释的问题,可能需要实现一种解决方法,可在此处找到: MKAnnotation未在iOS5中选中

我相信你现在已经找到了自己的解决方案,但我希望这在某种程度上可以提供丰富的信息.