2012-03-07 84 views
2

我在设置标题字幕的问题我的地标。iOS - MKPlacemark集标题问题

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 

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

回答

6

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

据我所知,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

我相信你到目前为止,我们已经找到了自己的解决方案,但我希望这在某种程度上能够提供信息。