我想删除我的地图的所有叠加在一点,我尝试了不同的方式,但它从来没有工作。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;
}
这就是我做的:) 非常感谢@Anna Karenina! – Dachmt 2011-06-16 18:27:07