2

i have one UIViewcontroller with a MKMapKit and a PickerView.Like as shown below image. UIViewcontroller and i have one plist which contains all co ordinates corresponding to the values in PickerView .So once i select any item from PickerView i will refresh the current Map and take the coordinates of new item (some times it may be 5 to 6 co-ordinates) and show that in map.

But for me i can see that "Annotations" are coming to map view but when i click another item the previous pins are still there in Map.

I want to show only annotations pins of selected PickerView item.Please help me

USE CASE: this is my plist file enter image description here

In my plist there one field called" categories" the items listed in Pickerview is teh total category list.

So when i scroll through PickerView i will check whether the selected item matching with any items in Plist category field.If its matching iam showing their co ordinates in map view.

But when i click "restuarents" category in PickerView i got following map which is correct.

enter image description here

and when i click "cluburi" category its supposed to show 2 Pins in Map instead of 3.Actually its not clearing the current "restuarent "Pin from the map .Like below imageenter image description here

So how do i clear the previous Pins in map before displaying new Pins?

This is my code in viewcontroller.m file

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [locations count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [locations objectAtIndex: row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

[_MAPVIEW setRegion:[_MAPVIEW region] animated:YES];

    for(int j=0;j<[[savedContentsarray valueForKey:@"category"] count];j++)
    {
        if([[[savedContentsarray valueForKey:@"category"] objectAtIndex:j] isEqualToString:[locations objectAtIndex: row]])
         {


              coordinate.latitude=[[latitude objectAtIndex:j] floatValue];
              coordinate.longitude=[[longitude objectAtIndex:j] floatValue];
              annotation = [[myAnnotation alloc] initWithCoordinate:coordinate title:[[savedContentsarray valueForKey:@"name"]objectAtIndex:j]];
              [_MAPVIEW addAnnotation:annotation];


        }

    }

}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {
    //7
    if([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    //8
    static NSString *identifier = @"myAnnotation";
    MKPinAnnotationView * annotationView = (MKPinAnnotationView*)[_MAPVIEW dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (!annotationView)
    {
        //9
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        annotationView.pinColor = MKPinAnnotationColorPurple;
        annotationView.animatesDrop = YES;
        annotationView.canShowCallout = YES;
    }else {
        annotationView.annotation = annotation;
    }
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    return annotationView;
}
Bangalore
  • 1,572
  • 4
  • 20
  • 50
  • 1
    So if I understood your requirement, you want only the selected item from picker view to be shown on the map. If that is so, then you many need to remove the existing pin from the Mapview sub views. I have a working piece of code, just give a moment to me, to put that here – Balram Tiwari Dec 14 '13 at 07:32
  • Ok that will be great!!, i have tried that removeAnnotation code once i have added that then the map always seems to be empty..thats y i removed.can u give your sample code – Bangalore Dec 14 '13 at 07:40

1 Answers1

1

So in my app, I have made a custom class inherited from MKAnnotationView as btMapAnnotation. You have to make this call at the time any object is selected in picker control.

for (NSObject *a in [mapView annotations]) {
    if ([a isKindOfClass:[btMapAnnotation class]]) {
        btMapAnnotation *annotation = (btMapAnnotation *)a;
        [mapView removeAnnotation:annotation];
    }
}

Once this call is done, then only add your new annotation to your Map.

Balram Tiwari
  • 5,657
  • 2
  • 23
  • 41
  • you mean in didselect method of pickerview – Bangalore Dec 14 '13 at 07:59
  • 1
    Yes, as soon as the picker item is tapped, the above code will try to find annotation if any & remove it. After that your code follows to add a new annotation with the selected coordinates from your plist file. – Balram Tiwari Dec 14 '13 at 08:00
  • @BalramTiwari Could you take a look at question related to MKOverlayRenderer and see if you have a solution, thanks http://stackoverflow.com/questions/20558591/animated-gif-not-working-in-mkmapview-overlay-using-mkoverlayrenderer?noredirect=1#comment30789828_20558591 – wigging Dec 15 '13 at 01:07