I would like to remove all the overlays of my map at one point, and I tried different ways but it never works.
Last try I did [self.mapView removeOverlays:self.mapView.overlays];
and it still doesn't work. Any idea how I can remove those overlays?
Thank you.
UPDATE 1
I have the error: malloc: *** error for object 0x5adc0c0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
I think I know why, but don't really know how to fix it... Here is the code when I need to draw another line on my 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;
So I release the routeLine
object which is the previous overlay. So when I tried to remove it, it crashes because it has already been deallocated.
Here is the code for the mapView delegate for adding overlay views:
- (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;
}
If you guys know how I can fix this issue, removing all overlays from my MKMapView it would be awesome!
UPDATE 2
I tried not releasing my routeLine
and routeLineView
objects and it works now. It seems like there are no leaks too. So now I'm doing this:
// 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;
}
When you call removeOverlays:
, the map view will release the MKOverlay and MKOverlayView objects.
You hold your own references to these in _routeLine
and _routeLineView
.
After removeOverlays:
is called, your variables will be pointing to already-released memory. When you re-create the polyline, you are over-releasing which then results in a crash.
So remove the release
calls:
if (_routeLine != nil) {
[_routeLine release]; // <-- remove this
self.routeLine = nil;
}
if (_routeLineView != nil) {
[_routeLineView release]; // <-- remove this
self.routeLineView = nil;
}