2013-09-04 5 views
0

Méthode d'annotation removeAnnotation n'est pas appelée, de sorte que les images sur la carte sont dupliquées lors de la mise à jour. Comment appeler la méthode removeAnnotation afin que nous puissions supprimer l'image de l'ancien emplacement.removeAnnotation méthode non appelée

<pre> 
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    static NSString *AnnotationViewID = @"annotationViewID"; 

    MKAnnotationView *annotationView = (MKAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID]; 

    if (annotationView == nil) 
    { 
     annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease]; 
    } 

    annotationView.image = [UIImage imageNamed:@"Bike.png"]; 
    annotationView.annotation = annotation; 

    return annotationView; 
} 

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    // here do 
    //UIImage *pinImage = [UIImage imageNamed:@"Bike.png"]; 
    CLLocation *currentLocation = newLocation; 
    if(currentLocation != nil) 
    { 
     CLLocationCoordinate2D location = CLLocationCoordinate2DMake(currentLocation.coordinate.latitude, currentLocation.coordinate.longitude); 
     MyAnnotation *annotation = [[MyAnnotation alloc] initWithCoordinates:location title:@"India" subTitle:@"Sarvopari Mall"]; 
     if (newLocation) 
     { 
      [self.myMapView removeAnnotation:annotation]; 
     } 
     [self.myMapView addAnnotation:annotation]; 
    } 
} 
</pre> 
+0

Essayez supprimer l'annotation dans la méthode viewForAnnotation –

Répondre

0
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation 
    { 
     static NSString *AnnotationViewID = @"annotationViewID"; 

     MKAnnotationView *annotationView = (MKAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID]; 

     if (annotationView == nil) 
     { 
      annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease]; 
     } 

     [self.myMapView removeAnnotation:annotation]; 
     annotationView.image = [UIImage imageNamed:@"Bike.png"]; 
     annotationView.annotation = annotation; 

     return annotationView; 
    } 
+0

Suppression de l'annotation alors que l'affichage de la carte demande car son point de vue n'est probablement pas une bonne idée. – Anna

0

En regardant le code ci-dessus, probablement [self.myMapView removeAnnotation:annotation]; sera appelé parce que if (newLocation) sera vrai que vous mettez à jour vers un nouvel emplacement. Sinon, dans le code ci-dessous, vous supprimez et ajoutez exactement la même annotation que vous pouvez voir.

if (newLocation) 
    { 
     [self.myMapView removeAnnotation:annotation]; 
    } 
    [self.myMapView addAnnotation:annotation]; 

Je pense que vous devriez plutôt créer une autre annotation avec le oldLocation et supprimer que l'on place.

2

Vous devez conserver l'annotation en tant que variable d'instance ou propriété lors de son ajout à la carte. Ensuite, lorsque l'emplacement est mis à jour, supprimez l'annotation précédente (stockée dans la variable d'instance) et ajoutez-en une nouvelle.

Vous devez modifier votre UIViewController comme ceci:

@interface YourViewController : UIViewController 
{ 
    MyAnnotation *annotation; 
} 

@end 

et modifier vos méthodes de délégué de LocationManager comme ceci:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{ 
    // here do 
    //UIImage *pinImage = [UIImage imageNamed:@"Bike.png"]; 
    CLLocation *currentLocation = newLocation; 
    if(currentLocation != nil) 
    { 
     //Removes previous annotation 
     if(annotation){ 
      [self.myMapView removeAnnonation:annotation]; 
     } 
     CLLocationCoordinate2D location = CLLocationCoordinate2DMake(currentLocation.coordinate.latitude, currentLocation.coordinate.longitude); 
     //Keeps annotation in an instance variable 
     annotation = [[MyAnnotation alloc] initWithCoordinates:location title:@"India" subTitle:@"Sarvopari Mall"]; 
     [self.myMapView addAnnotation:annotation]; 
    } 
} 
+0

Oui et si la propriété de coordonnées est déclarée en écriture, vous pouvez simplement mettre à jour les coordonnées de l'ivar au lieu de les supprimer et de les ajouter à nouveau. – Anna

+0

Merci Bilal - Code Works mais à cause de j'ai des crédits faibles, je ne peux pas voter pour vous :( –

Questions connexes