3

I have a UITableView and each cell contains a Map button. When I tap each button, Map view is shown and corresponding Annotation pin is added.

This code is called when I tap each map button,

    double tempLatitude  = [[[myDict objectForKey:key] objectAtIndex:15] doubleValue];
    double tempLongitude = [[[myDict objectForKey:key] objectAtIndex:16] doubleValue];
                                                  // key is the cell index.

         //--** Setting the Latitude and Longitude 
         CLLocationCoordinate2D myCoordinate;

         myCoordinate.latitude  = tempLatitude;
         myCoordinate.longitude = tempLongitude;

         MyMapAnnotation *MyAnnotation = [[[MyMapAnnotation alloc] initWithCoordinate:myCoordinate addressDictionary:nil]autorelease];
         MyAnnotation.title            = [[myDict objectForKey:key] objectAtIndex:1];
         MyAnnotation.subtitle         = [NSString  stringWithFormat:@"%f %f", MyAnnotation.coordinate.latitude, MyAnnotation.coordinate.longitude];

And this is the code of MyMapAnnotation,

    @synthesize coordinate = theCoordinate;
    @synthesize title = theTitleAnnotation;
    @synthesize subtitle = theSubTitleAnnotation;



    - (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate addressDictionary:(NSDictionary *)addressDictionary 
    {

      if ((self = [super initWithCoordinate:coordinate addressDictionary:addressDictionary]))
      {
         self.coordinate = coordinate;
      }
         return self;
    }

Everything works fine. Now, when I tap one button, corresponding annotation pin is shown and when I tap second button, the previous pin is still there and second annotation pin is also being added.

But

What I want is,

When I tap each button, the previous annotation pin should be removed and only the current annotation pin should be added.

How can I identify the previous pin?

And where can I give this code [mapView removeAnnotation:annotationToRemove] ??

EDIT: Obviously, the 'previous annotation pin' is the 'annotationToRemove'. I know this. My question is, How can I identify this previous annotation pin to specify as 'annotationToRemove'??

Thanks :-)

S.Philip
  • 461
  • 7
  • 24

5 Answers5

7

have you try with [myMap removeAnntoation:[myMap.annotations objectAtIndex:0]]; ?

EDIT: There are several ways to do this, one is to set a tag to the annotation, then search it by tag; another way is to check all annotations and then remove that which you want, if your problem is to remove the previous annotation added, you can call:

[myMap removeAnntoation:[myMap.annotations lastObject]];

P.S: if you have myMap.showsUserLocation=YES; look this: How to remove all annotations from MKMapView without removing the blue dot?.

Community
  • 1
  • 1
Mat
  • 7,613
  • 4
  • 40
  • 56
  • 1
    I found the solution by giving this code before adding the next annotation pin, if([self.mapView.annotations count] == 1) { [self.mapView removeAnnotation:[self.mapView.annotations objectAtIndex:0]]; } .Thanks :) – S.Philip Jul 14 '11 at 14:32
  • As my P.S, if you show the user location on the mapview your "if" it could be true even if there is no annotation added by you but there is just the user location annotation(the blue circle)..cheers! – Mat Jul 14 '11 at 14:44
1

If you are showing the Mapview in Another View means just use

-(void)viewWillappear
{
[myMap removeAnnotations:toRemove];
}
Aravindhan
  • 15,608
  • 10
  • 56
  • 71
1

You can use [MyAnnotation setHidden:YES]; to turn the pin off and turn it on again later.

0

try this

-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view 
{
   [mapView removeAnnotation:annotationToRemove]
}
Mat
  • 7,613
  • 4
  • 40
  • 56
Arun
  • 2,271
  • 1
  • 14
  • 18
0

This one worked for me on Swift 5:

mapView.removeAnnotations([mapView.annotations].last)
Andy
  • 751
  • 1
  • 12
  • 25