2

I'm having issues setting the title and subtitle of my placemark.

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"; and placemark.subtitle = @"Some subtitle";

give me an error of:

Assigning to property with 'readonly' attribute not allowed

Why can I not set the Title and Subtitle here?

Romes
  • 3,088
  • 5
  • 37
  • 52

1 Answers1

8

Thought I'd wake this thread up and give you a solution that I came up with.

As far as I'm aware, MKPlacemark's title/subtitle are readonly properties due to inherent assignment. However, with the solution I found, you can simply pass your MKPlacemark into an MKPointAnnotation as follows:

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

Please note that the final [self.mapView selectAnnotation:point animated:NO]; is a workaround to allow automatic popping-up of the placemark. However, the animated:BOOL portion only seems to work with NO in iOS5 - You might want to implement a workaround if you experience issues manually popping up the point-annotation, which can be found here: MKAnnotation not getting selected in iOS5

I'm sure you've already found your own solution by now, but I hope this is, in some way, informative.

Community
  • 1
  • 1
Sarreph
  • 1,986
  • 2
  • 19
  • 41