我想刪除我的地圖的所有疊加在一點,我嘗試了不同的方式,但它從來沒有工作。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