iOS删除MKMapView叠加不起作用
我想删除我的地图的所有叠加在一点,我尝试了不同的方式,但它从来没有工作。iOS删除MKMapView叠加不起作用
最后尝试我做了[self.mapView removeOverlays:self.mapView.overlays];
它仍然无法正常工作。任何想法如何我可以删除这些叠加?
谢谢。
更新1
我有错误:malloc: *** error for object 0x5adc0c0: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug
我想我知道为什么,但真的不知道如何解决它... 下面是代码,当我需要动用我的MapView的另一条线:
// Create a c array of points.
MKMapPoint *pointsArray = malloc(sizeof(CLLocationCoordinate2D) * 2);
// Create 2 points.
MKMapPoint startPoint = MKMapPointForCoordinate(CLLocationCoordinate2DMake(oldLatitude, oldLongitude));
MKMapPoint endPoint = MKMapPointForCoordinate(CLLocationCoordinate2DMake(newLatitude, newLongitude));
// Fill the array.
pointsArray[0] = startPoint;
pointsArray[1] = endPoint;
// Erase polyline and polyline view if not nil.
if (self.routeLine != nil) {
[_routeLine release];
self.routeLine = nil;
}
if (self.routeLineView != nil) {
[_routeLineView release];
self.routeLineView = nil;
}
// Create the polyline based on the array of points.
self.routeLine = [MKPolyline polylineWithPoints:pointsArray count:2];
// Add overlay to map.
[self.mapView addOverlay:self.routeLine];
// clear the memory allocated earlier for the points.
free(pointsArray);
// Save old coordinates.
oldLatitude = newLatitude;
oldLongitude = newLongitude;
所以我释放routeLine
对象,它是以前的覆盖。所以当我试图删除它时,它会崩溃,因为它已经被释放。
下面是MapView的委托的代码添加叠加观点:
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
MKOverlayView* overlayView = nil;
if(overlay == _routeLine) {
// If we have not yet created an overlay view for this overlay, create it now.
if(self.routeLineView == nil) {
self.routeLineView = [[[MKPolylineView alloc] initWithPolyline:_routeLine] autorelease];
self.routeLineView.fillColor = [UIColor blueColor];
self.routeLineView.strokeColor = [UIColor blueColor];
// Size of the trace.
self.routeLineView.lineWidth = routeLineWidth;
}
overlayView = self.routeLineView;
}
return overlayView;
}
如果你们知道我怎么能解决这个问题,除去我的MKMapView全部覆盖这将是真棒!
更新2
我试过不释放我的routeLine
和routeLineView
对象和现在的工作。似乎也没有泄漏。所以现在我正在这样做:
// Erase polyline and polyline view if not nil.
if (self.routeLine != nil) {
//[_routeLine release];
self.routeLine = nil;
}
if (self.routeLineView != nil) {
//[_routeLineView release];
self.routeLineView = nil;
}
当你调用removeOverlays:
,地图视图将发布MKOverlay和MKOverlayView对象。
您在_routeLine
和_routeLineView
中拥有自己对这些的引用。
在调用removeOverlays:
后,您的变量将指向已释放的内存。当您重新创建多段线时,您会过度释放,然后导致崩溃。
所以除去release
呼叫:
if (_routeLine != nil) {
[_routeLine release]; // <-- remove this
self.routeLine = nil;
}
if (_routeLineView != nil) {
[_routeLineView release]; // <-- remove this
self.routeLineView = nil;
}
当您在调试器中逐步执行代码时,哪里出现错误?
有人认为,您的保留/发布周期可能存在self.routeLine
和self.routeLineView
的问题。假设他们有retain
属性的属性,当你做
self.routeLine = [MKPolyline polylineWithPoints:pointsArray count:2];
您合成存取被保留新MKPolyline
对象。该对象还会有一个来自创建它的便捷方法的保留/自动释放对。当这个方法被再次调用,并调用
if (self.routeLine != nil) {
[_routeLine release];
self.routeLine = nil;
}
代码,你最终会释放变量两次,第一次用明确[_routeLine release]
呼叫和第二与上self.routeLine = nil;
呼叫合成的访问。它会留在内存中,但是当你的自动释放池被耗尽时会崩溃。
在大多数情况下,要清除所有覆盖的MKMapView
(本示例中名为mapView
),我会做类似如下:
for (id<MKOverlay> overlayToRemove in mapView.overlays)
{
if ([overlayToRemove isKindOfClass:[OverlayClassToRemove class]])
{
[mapView removeOverlay:overlayToRemove];
}
}
的'polylineWithPoints:计数:'方法需要MKMapPoint结构的C数组。 – Anna 2011-06-16 17:40:49
我喜欢去除你告诉我的覆盖图的方式,并认为它会起作用,或者至少不会使应用程序崩溃。但它确实和我有同样的错误。 @Anna Karenina我存储积分的方式是正确的吗?我尝试存储在一个'NSArray'中,无论如何都不能添加这种类型的对象。 – Dachmt 2011-06-16 17:48:00
@Dachmt和@Anna Karenina我表态改正了。这就是我所猜测的事情,我没有做过自己。我会编辑我的答案。 – Kongress 2011-06-16 17:56:31
夫特3 extenstion
extension MKMapView
{
func removeAllOverlay(){
for overlay:MKOverlay in self.overlays {
self.remove(overlay)
}
}
}
这就是我做的:) 非常感谢@Anna Karenina! – Dachmt 2011-06-16 18:27:07