2011-10-21 23 views
10

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

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

答えて

13

かなり古い質問、多分同じ問題の際に他の誰かがつまずく(私が行ったように):

マップの注釈に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]; 

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

5

次のコードは、iOS 5.1

-(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]; 
    } 
}]; 
} 
でCLGeocoderを使用して地図上の注釈を配置する実証します