2

目印のタイトルサブタイトルの設定に問題があります。

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

ここでタイトルとサブタイトルを設定できないのはなぜですか?

4

1 に答える 1

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

[self.mapView selectAnnotation:point animated:NO];ファイナルは、目印の自動ポップアップを許可するための回避策であることに注意してください。ただし、このanimated:BOOL部分NOは iOS5 でのみ動作するようです。ポイント アノテーションを手動でポップアップする際に問題が発生した場合は、回避策を実装することをお勧め します。

すでに独自の解決策を見つけていると思いますが、これが何らかの形で有益であることを願っています.

于 2012-07-06T20:05:30.530 に答える