在地图视图中添加和删除MKAnnotation

时间:2012-05-10 21:04:25

标签: iphone objective-c ios cocoa-touch ios5

我有一个应用程序,我使用MapView,我有4种类型的MKAnnotation。我屏幕上还有4个按钮。每个按钮应显示或隐藏其中一个MKannotation类型。 我能够跟踪类型并删除它们。但是当我尝试添加任何MKannotation时,我收到一条错误消息。在搜索之后我发现了一个类似的问题,但没有得到解答。

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

首先,我在调用网络服务后添加MKAnnotation:

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

现在我在男人视图上有4个按钮。 Type1,Type2,Type3和Type4按钮。 如果我单击Type1按钮,它将删除Type1的所有MKAnnotation,使用以下代码完美地运行:

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

现在,如果我想再次显示Type1,我使用以下代码导致问题:

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

此问题出现在此行中     [mapView addAnnotation:map]; 这导致我提到的错误消息relier(这里又是)

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.

2 个答案:

答案 0 :(得分:0)

现在有效,问题在于另一种方法。 我试图通过这样做将双值转换为NSString:

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

%d应该是%f !!!!!那是一个愚蠢的小错误。 感谢大家的帮助和@AnnaKarenina的提示

答案 1 :(得分:0)

// REMOVING ALL ANNOTATION
    for (id <MKAnnotation>  myAnnot in [objMapView annotations])
    {
        if (![myAnnot isKindOfClass:[MKUserLocation class]])
        {
            [objMapView removeAnnotation:myAnnot];
        }
    }