Hiren Patel
Hiren Patel

Reputation: 69

removeAnnotation method not called

Annotation method removeAnnotation is not called, so that the images on map duplicated while update location. How to call removeAnnotation method so that we can remove image from old location.

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

Upvotes: 0

Views: 195

Answers (3)

Bilal Saifudeen
Bilal Saifudeen

Reputation: 1677

You should keep the annotation as an instance variable or property while adding it to map. Then while location updates, remove the previous annotation(stored in instance variable) and add new.

You should modify your UIViewController like this:

@interface YourViewController : UIViewController
{
    MyAnnotation *annotation;
}

@end

And modify your LocationManager Delegate methods like this:

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

Upvotes: 2

Manuel Escrig
Manuel Escrig

Reputation: 2835

From looking at the code above, probably [self.myMapView removeAnnotation:annotation]; will be called because if (newLocation) will be true as you are updating to a new location.

Otherwise, in the code below you are removing and adding exactly the same annotation as you can see.

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

I think you should instead create another annotation with the oldLocation and remove that one instead.

Upvotes: 0

Bhavesh Nayi
Bhavesh Nayi

Reputation: 3656

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

Upvotes: 0

Related Questions