12

マップビューは正常に動作していますが、マップに配置されたピンのタイトルは United States です。このタイトルを変更するにはどうすればよいですか?

    MKCoordinateRegion thisRegion = {{0.0,0.0}, {0.0,0.0}};

        thisRegion.center.latitude = 22.569722;
        thisRegion.center.longitude = 88.369722;

        CLLocationCoordinate2D coordinate;
        coordinate.latitude = 22.569722;
        coordinate.longitude = 88.369722;

        thisRegion.center = coordinate;

        MKPlacemark *mPlacemark = [[[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil] autorelease];

        [mapView addAnnotation:mPlacemark];
        [mapView setRegion:thisRegion animated:YES];
4

3 に答える 3

15

かなり古い質問ですが、他の誰かが同じ問題に遭遇する可能性があります(私がしたように):

マップの注釈に MKPlacemark を追加しないでください。代わりに MKPointAnnotation を使用してください。このクラスには、読み取り専用ではないタイトルとサブタイトルのプロパティがあります。それらを設定すると、それに応じてマップ上の注釈が更新されます。これはおそらくあなたが望むものです。

コードで MKPointAnnotation を使用するには、MKPlacemark を割り当てて追加する行を次のコードに置き換えます。

MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = coordinate;
annotation.title = NSLocalizedString(@"Dropped Pin",
                                     @"Title of a dropped pin in a map");
[mapView addAnnotation:annotation];

タイトルとサブタイトルのプロパティは、後でいつでも設定できます。たとえば、非同期アドレス クエリを実行している場合、アドレスが利用可能になるとすぐにサブタイトルをアノテーションのアドレスに設定できます。

于 2012-10-31T08:55:31.850 に答える
5

以下のコードは、iOS 5.1 で CLGeocoder を使用してマップに注釈を配置する方法を示しています。

-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

CLGeocoder *geocoder = [[CLGeocoder alloc] init];

// Apple recommendation - if location is older than 30s ignore
// Comment out below during development
/*  if (fabs([newLocation.timestamp timeIntervalSinceDate:[NSDate date]]) > 30) {
    NSLog(@"timestamp");
    return;
}*/   

CLLocation *coord = [[CLLocation alloc] initWithLatitude:locationManager.location.coordinate.latitude longitude:locationManager.location.coordinate.longitude];                            
[geocoder reverseGeocodeLocation:coord completionHandler:^(NSArray *placemarks, NSError *error) {

    if (error) {
        NSLog(@"Geocode failed with error");
    }

    // check for returned placemarks
    if (placemarks && placemarks.count > 0) {
        CLPlacemark *topresult = [placemarks objectAtIndex:0];
        MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
        annotation.coordinate = locationManager.location.coordinate;
        annotation.title = NSLocalizedString(@"You are here", @"Title");
        annotation.subtitle = [NSString stringWithFormat:@"%@, %@", [topresult subAdministrativeArea], [topresult locality]];
        [self.mapView addAnnotation:annotation];
    }
}];
}
于 2012-12-07T21:52:33.697 に答える
1

MKPlacemark が準拠している MKAnnotation プロトコルを確認してください。タイトルを設定できるはずです。

http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKAnnotation_Protocol/Reference/Reference.html

http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKPlacemark_Class/Reference/Reference.html

于 2011-10-21T19:25:23.017 に答える