2012-03-07 73 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

我相信你到目前爲止,我們已經找到了自己的解決方案,但我希望這在某種程度上能夠提供信息。