2010-12-01 5 views
7

En mode Carte, je montre l'emplacement actuel de l'utilisateur. Sur le clic sur l'épingle son affichage "Current Location". Je veux le changer en "My Current Location". Comment puis-je le changer. Je souhaite également modifier la couleur de la broche de l'emplacement actuel de l'utilisateur dans une minuterie. Quelque chose comme chaque seconde, il devrait changer sa couleur entre le vert, le violet et le rouge. Possible de le faire?iPad Mapkit - Modifier le titre de "Emplacement actuel"

J'utilise l'emplacement par défaut de show kit de carte, puis manipuler la broche annotation couleur comme ci-dessous:

- (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

Montrez comment vous montrant l'emplacement actuel. Utilisez-vous la propriété showsUserLocation de map view pour obtenir le point bleu par défaut ou créer une pin personnalisée? Montrez comment vous ajoutez l'annotation et la méthode viewForAnnotation. – Anna

Répondre

20

Si vous laissez la voir carte spectacle la vue par annotation par défaut pour l'emplacement de l'utilisateur (point bleu), elle est plus simple à mettre en œuvre (et vous obtenez un joli point bleu avec un joli cercle de zoom animé).

Si vous devez montrer l'emplacement de l'utilisateur en utilisant une image de broche au lieu d'un point bleu, alors un travail supplémentaire est nécessaire.

Tout d'abord, la manière simple en utilisant le point bleu:

- (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 vous voulez utiliser votre vue d'annotation personnalisée pour l'emplacement de l'utilisateur à la place, vous devez mettre le code de changement de couleur des broches dans la vue personnalisée . Une façon de changer périodiquement la couleur est d'utiliser performSelector: withObject: afterDelay :. Dans le SolarAnnotationView.m, ajouter ces deux méthodes:

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

ajouter également les en-têtes de méthode dans le fichier SolarAnnotationView.h.

changer ensuite la méthode viewForAnnotation comme ceci:

- (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

Merci beaucoup pour l'information. – Satyam

Questions connexes