2010-09-07 8 views
11

Estoy cargando una anotación en la vista de mi mapa. La anotación se muestra como un pin cuando se carga el mapa.Cómo mostrar automáticamente el título/subtítulo en la anotación del mapa (pin)

Sin embargo, el título y los subtítulos no aparecen automáticamente en el pin. Actualmente, el usuario debe tocar el pin antes de que se muestre el título.

¿Hay alguna manera de hacer que el título se muestre automáticamente en el pin cuando se carga el mapa?

(Esta pregunta es casi lo mismo, pero no está allí: To display the title for the current loaction in map in iphone porque ya tengo los atributos -TITLE y -subtitle definidos en mi objeto.)

Gracias

Respuesta

16

El método para llamar es "selectAnnotation: animated" de MKMapView.

9
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views 
{  
    MKAnnotationView *annotationView = [views objectAtIndex:0]; 
    id<MKAnnotation> mp = [annotationView annotation]; 
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate] ,350,350); 

    [mv setRegion:region animated:YES];  

    [mapView selectAnnotation:mp animated:YES]; 

} 

si se está haciendo lo mismo que está llamando el método setRegion, a continuación, asegúrese de que usted llama

[mapView selectAnnotation:mp animated:YES]; 

después

[mv setRegion:region animated:YES];  
0

A partir de IOS 11, hay una nuevo tipo de MKAnnotationView llamado MKMarkerAnnotationView, que puede mostrar el título y el subtítulo sin ser seleccionado. Check https://developer.apple.com/documentation/mapkit/mkmarkerannotationview

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 

    guard !(annotation is MKUserLocation) else { 
     return nil 
    } 

    if #available(iOS 11.0, *) { 
     let annoView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "HunterAnno") 
     annoView.canShowCallout = true 
     return annoView 
    } 

    let annoView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "HunterAnnoLow") 
    annoView.canShowCallout = true 
    return annoView 
} 
+1

Si bien este enlace puede responder a la pregunta, es mejor incluir las partes esenciales de la respuesta aquí y proporcionar el enlace de referencia. Las respuestas de solo enlace pueden dejar de ser válidas si la página vinculada cambia. - [De la crítica] (/ review/low-quality-posts/17880863) – stdunbar

Cuestiones relacionadas