Iphone 在地图视图中添加和删除注释
我有一个使用MapView的应用程序,我有4种类型的MKAnnotation。我在屏幕上还有4个按钮。每个按钮都应显示或隐藏其中一种注释类型。 我可以跟踪类型并删除它们。但是当我尝试添加任何MKannotation时,会收到一条错误消息。搜索之后,我发现了一个类似的问题,但没有得到回答Iphone 在地图视图中添加和删除注释,iphone,objective-c,ios,cocoa-touch,ios5,Iphone,Objective C,Ios,Cocoa Touch,Ios5,我有一个使用MapView的应用程序,我有4种类型的MKAnnotation。我在屏幕上还有4个按钮。每个按钮都应显示或隐藏其中一种注释类型。 我可以跟踪类型并删除它们。但是当我尝试添加任何MKannotation时,会收到一条错误消息。搜索之后,我发现了一个类似的问题,但没有得到回答 An instance 0x6ec5750 of class MapPoint was deallocated while key value observers were still registered
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.
首先,我在调用web服务后从添加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];
}
现在起作用了,问题完全在于另一种方法。 我试图通过以下操作将double value转换为NSString:
[Nsstring stringWithFormat:@"%d",doubleVal];
%d应该是%f!!!!!那是一个愚蠢的小错误。
感谢所有人的帮助,@AnnaKarenina给出提示//删除所有注释
// REMOVING ALL ANNOTATION
for (id <MKAnnotation> myAnnot in [objMapView annotations])
{
if (![myAnnot isKindOfClass:[MKUserLocation class]])
{
[objMapView removeAnnotation:myAnnot];
}
}
for(id myAnnot在[objMapView注释]中)
{
如果(![myAnnot不是Kindof类:[MKUserLocation类]])
{
[objMapView removeAnnotation:myAnnot];
}
}
错误消息是怎么说的?是的,错误消息到底是什么,并在添加注释的地方张贴代码。如果错误为“在键值观察者仍然注册时解除分配”,则可能会发生这种情况,因为注释的坐标无效。错误与链接中的错误相同。我已经用错误消息更新了问题。您是否在不应该释放MapPoint的时候释放了MapPoint?是否添加了一个观察者来收听按钮按下?在第一个for循环中,坐标是使用{pr.promotionLatitude,pr.promotionLongitude}
设置的。在第三个for循环中,使用{[pr.promotionLatitude doubleValue],[pr.promotionLatitude doubleValue]}
设置坐标,即使pr
对象似乎是相同的Promotion
类型。为什么第三个for循环使用的是doubleValue
?你能在第三个for循环中记录newCoord的值吗?
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.
[Nsstring stringWithFormat:@"%d",doubleVal];
// REMOVING ALL ANNOTATION
for (id <MKAnnotation> myAnnot in [objMapView annotations])
{
if (![myAnnot isKindOfClass:[MKUserLocation class]])
{
[objMapView removeAnnotation:myAnnot];
}
}