2011-10-21 22 views
10

Tengo mi mapview funcionando bien, pero el pin que se coloca en el mapa tiene el título Estados Unidos. ¿Cómo puedo cambiar este título?MKPlacemark pin title

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

Respuesta

13

pregunta bastante antiguo, pero tal vez alguien más se topa con el mismo problema (como yo):

No agregue un MKPlacemark a las anotaciones del mapa; use MKPointAnnotation en su lugar. Esta clase tiene propiedades de título y subtítulo que no son de solo lectura. Cuando los configura, la anotación en el mapa se actualiza en consecuencia, y esto es probablemente lo que desea.

Para utilizar MKPointAnnotation en su código, reemplace las líneas que asignan y añadir la MKPlacemark con este código:

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

puede establecer el título y las propiedades del subtítulo cualquier momento posterior, también. Por ejemplo, si tiene una consulta de dirección asíncrona ejecutándose, puede configurar los subtítulos en la dirección de la anotación tan pronto como la dirección esté disponible.

5

El código siguiente muestra la colocación de una anotación en un mapa mediante CLGeocoder en 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]; 
    } 
}]; 
}