Search code examples
iphoneobjective-cioscocoa-touchios5

adding and removing MKAnnotation from a map view


i have an app where i use MapView and i have 4 types of MKAnnotation. i have also 4 buttons on the screen. each button should show or hide one of the MKannotation types. i'm able to track the types and remove them.but when i try to add any MKannotation i get an error message. after searching i found a similar problem which has not been answered.

An instance 0x6ec5750 of class MapPoint was deallocated while key value observers were    still registered with it. Observation info was leaked, and may even become mistakenly attached  to some other object.

ios5 removing annotation from a mapview

first of all i'm adding the MKAnnotation from after calling a web service:

for (int x=0; x<PromotionsArray.count; x++) 
{
    Promotion *pr = [PromotionsArray objectAtIndex:x];
    CLLocationCoordinate2D newCoord ={ pr.promotionLatitude, pr.promotionLongitude};
    MapPoint *mp= [[MapPoint alloc] initWithCoordinate:newCoord];
    mp.currentTitle=pr.PromotionTitle;
    mp.currentSubTitle=pr.RetailerName;
    mp.currentPromotionArrayID=[NSString stringWithFormat:@"%d",x];
    mp.currentRetailerID = pr.RetailerID;
    mp.currentPromotionType = pr.PromotionType;
    [mapView addAnnotation:mp];
}

now i have 4 buttons on the man view. Type1,Type2,Type3 and Type4 button. if i click on Type1 button it will remove all the MKAnnotation of Type1, using the following code which works perfectly:

for (id <MKAnnotation>  myAnnot in [mapView annotations])
    {
        if (![myAnnot isKindOfClass:[MKUserLocation class]])
        {
            if([(MapPoint *)myAnnot currentPromotionType]==PromotionType1)
            {
                NSLog(@"Hiding All Type1 Annotations");
                [mapView removeAnnotation:myAnnot];
            }
        }
    }

now if i want to show the Type1 again, i use the following code which cause the problem:

    for (int x=0; x<PromotionsArray.count; x++) 
    {
        Promotion *pr = [PromotionsArray objectAtIndex:x];
        if (pr.promotionType==PromotionType1) 
        {
            CLLocationCoordinate2D newCoord ={ [pr.promotionLatitude doubleValue],[pr.promotionLongitude doubleValue]};
            MapPoint *map= [[MapPoint alloc] initWithCoordinate:newCoord];
            map.currentTitle=pr.PromotionTitle;
            map.currentSubTitle=pr.RetailerName;
            map.currentPromotionArrayID=[NSString stringWithFormat:@"%d",x];
            [mapView addAnnotation:map];
        }
    }

the problem appears in this line [mapView addAnnotation:map]; which causes the error message i mentioned relier(here is it again)

An instance 0x6ec5750 of class MapPoint was deallocated while key value observers were    still registered with it. Observation info was leaked, and may even become mistakenly attached  to some other object.

Solution

  • works now, the problem is in totally another method. i was trying to convert double value to NSString by doing this:

    [Nsstring stringWithFormat:@"%d",doubleVal]; 
    

    the %d should be %f !!!!! that was a stupid small mistake. thanks all for the help and @AnnaKarenina for your hint