2010-12-01 17 views
7

En la vista del mapa, estoy mostrando la ubicación actual del usuario. Al hacer clic en el pin, se muestra "Ubicación actual". Quiero cambiarlo a "Mi ubicación actual". ¿Cómo puedo cambiarlo? También quiero cambiar el color del pin de ubicación de usuario actual en un temporizador. Algo así como cada segundo debe cambiar su color entre verde, morado y rojo. Posible hacerlo?iPad Mapkit - Cambia el título de "Ubicación actual"

estoy usando ubicación mostrar por defecto mapa del kit y luego manipular el color pin anotación de la siguiente manera:

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation{ 
static NSString *AnnotationViewID = @"annotationViewID"; 
SolarAnnotationView* annotationView = (SolarAnnotationView*)[map dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID]; 
if(annotationView == nil) 
{ 
    if([self CLLocationCoordinate2DEquals:mapView.userLocation.location.coordinate withSecondCoordinate:[annotation coordinate]]) //Show current location with green pin 
    { 
     annotationView = [[SolarAnnotationView alloc] initWithAnnotation:annotation]; 
     annotationView.delegate = self; 
     [annotationView setPinColor:MKPinAnnotationColorGreen]; 
    } 
    else 
    { 
     annotationView = [[SolarAnnotationView alloc] initWithAnnotation:annotation]; 
     annotationView.delegate = self; 
    } 
} 

return annotationView; 

}

- (BOOL) CLLocationCoordinate2DEquals:(const CLLocationCoordinate2D)lhs withSecondCoordinate:(const CLLocationCoordinate2D) rhs{ 
const CLLocationDegrees DELTA = 0.001; 
return fabs(lhs.latitude - rhs.latitude) <= DELTA && fabs(lhs.longitude - rhs.longitude) <= DELTA; 

}

+0

Mostrar cómo se está mostrando la ubicación actual. ¿Está utilizando la propiedad showsUserLocation de la vista de mapa para obtener un punto azul predeterminado o crear un pin personalizado? Muestre cómo está agregando la anotación y el método viewForAnnotation. – Anna

Respuesta

20

Si deja que el mapa de vista de presentación la vista de anotación predeterminada para la ubicación del usuario (punto azul), es más fácil de implementar (y obtienes un bonito punto azul con un fantástico círculo de zoom animado).

Si debe mostrar la ubicación del usuario utilizando una imagen de pin en lugar de un punto azul, entonces se necesita algo más de trabajo.

En primer lugar, la forma más sencilla con el punto azul:

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation{ 
    if ([annotation isKindOfClass:[MKUserLocation class]]) 
    { 
     ((MKUserLocation *)annotation).title = @"My Current Location"; 
     return nil; //return nil to use default blue dot view 
    } 

    //Your existing code for viewForAnnotation here (with some corrections)... 
    static NSString *AnnotationViewID = @"annotationViewID"; 
    SolarAnnotationView* annotationView = (SolarAnnotationView*)[map dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID]; 
    if(annotationView == nil) 
    { 
     { 
      annotationView = [[[SolarAnnotationView alloc] initWithAnnotation:annotation] autorelease]; 
      //added autorelease above to avoid memory leak 
      annotationView.delegate = self; 
     } 
    } 

    //update annotation in view in case we are re-using a view 
    annotationView.annotation = annotation; 

    return annotationView; 
} 


Si desea utilizar su vista de anotación personalizado para la ubicación del usuario en su lugar, usted debe poner el código de color cambiante alfiler en la vista personalizada . Una forma de cambiar el color periódicamente es utilizando performSelector: withObject: afterDelay :. En el SolarAnnotationView.m, agregar estos dos métodos:

-(void)startChangingPinColor 
{ 
    switch (self.pinColor) { 
     case MKPinAnnotationColorRed: 
      self.pinColor = MKPinAnnotationColorGreen; 
      break; 
     case MKPinAnnotationColorGreen: 
      self.pinColor = MKPinAnnotationColorPurple; 
      break; 
     default: 
      self.pinColor = MKPinAnnotationColorRed; 
      break; 
    } 
    [self performSelector:@selector(startChangingPinColor) withObject:nil afterDelay:1.0]; 
} 

-(void)stopChangingPinColor 
{ 
    [NSObject cancelPreviousPerformRequestsWithTarget:self]; 
} 

añadir también las cabeceras de método para el archivo SolarAnnotationView.h.

a continuación, cambiar el método viewForAnnotation así:

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation{ 
    static NSString *AnnotationViewID = @"annotationViewID"; 
    SolarAnnotationView* annotationView = (SolarAnnotationView*)[map dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID]; 
    if(annotationView == nil) 
    { 
     { 
      annotationView = [[[SolarAnnotationView alloc] initWithAnnotation:annotation] autorelease]; 
      annotationView.delegate = self; 
     } 
    } 

    //Update annotation in view in case we are re-using a view... 
    annotationView.annotation = annotation; 

    //Stop pin color changing in case we are re-using a view that has it on 
    //and this annotation is not user location... 
    [annotationView stopChangingPinColor]; 

    if([self CLLocationCoordinate2DEquals:mapView.userLocation.location.coordinate withSecondCoordinate:[annotation coordinate]]) //Show current location with green pin 
    { 
     [annotationView setPinColor:MKPinAnnotationColorGreen]; 
     annotationView.canShowCallout = YES; 
     ((MKPointAnnotation *)annotation).title = @"My Current Location"; 
     [annotationView startChangingPinColor]; 
    } 

    return annotationView; 
} 
+1

Muchas gracias por la información. – Satyam

Cuestiones relacionadas