iOS删除MKMapView覆盖不起作用

Dac*_*hmt 10 overlay mkmapview ios android-mapview

我想在一点上删除我的地图的所有叠加,我尝试了不同的方式,但它永远不会工作.

最后一次尝试我做了[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;
Run Code Online (Sandbox Code Playgroud)

所以我释放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;
}
Run Code Online (Sandbox Code Playgroud)

如果你们知道如何解决这个问题,从我的MKMapView中删除所有叠加层就会很棒!

更新2

我试着不释放我routeLinerouteLineView对象,它现在有效.似乎也没有泄漏.所以现在我这样做:

// 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;
}
Run Code Online (Sandbox Code Playgroud)

小智 11

当您调用时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;
}
Run Code Online (Sandbox Code Playgroud)


Kon*_*ess 7

当您单步执行调试器中的代码时,错误出现在哪里?

一个想法,你可以有你的保留/释放周期的问题self.routeLineself.routeLineView.假设它们是属性retain属性,当你这样做时

self.routeLine = [MKPolyline polylineWithPoints:pointsArray count:2];  
Run Code Online (Sandbox Code Playgroud)

您的合成访问者保留新MKPolyline对象.该对象还将从创建它的方便方法中获得保留/自动释放对.当再次调用此方法并调用时

if (self.routeLine != nil) {
    [_routeLine release];
    self.routeLine = nil;
}
Run Code Online (Sandbox Code Playgroud)

代码,您将最终释放变量两次,第一次使用显式[_routeLine release]调用,第二次使用调用的合成访问器self.routeLine = nil;.它将保留在内存中,但在您的自动释放池耗尽时会使应用程序崩溃.

在大多数情况下,要清除MKMapView(mapView在此示例中命名)的所有叠加层,我会执行以下操作:

for (id<MKOverlay> overlayToRemove in mapView.overlays)
{
   if ([overlayToRemove isKindOfClass:[OverlayClassToRemove class]])
   {
       [mapView removeOverlay:overlayToRemove];
   }
}
Run Code Online (Sandbox Code Playgroud)