iOS删除MKMapView覆盖不起作用

前端之家收集整理的这篇文章主要介绍了iOS删除MKMapView覆盖不起作用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在一个地方删除我的地图的所有叠加层,我尝试了不同的方式,但它从来没有工作.

最后尝试我[self.mapView removeOverlays:self.mapView.overlays];它仍然不起作用.任何想法如何删除这些叠加?

谢谢.

更新1

我有错误:malloc:***错误的对象0x5adc0c0:指针被释放没有分配
***在malloc_error_break中设置一个断点来进行调试

我想我知道为什么,但不知道如何解决它?
以下是我需要在mapView上绘制另一行的代码

  1. // Create a c array of points.
  2. MKMapPoint *pointsArray = malloc(sizeof(CLLocationCoordinate2D) * 2);
  3.  
  4. // Create 2 points.
  5. MKMapPoint startPoint = MKMapPointForCoordinate(CLLocationCoordinate2DMake(oldLatitude,oldLongitude));
  6. MKMapPoint endPoint = MKMapPointForCoordinate(CLLocationCoordinate2DMake(newLatitude,newLongitude));
  7.  
  8. // Fill the array.
  9. pointsArray[0] = startPoint;
  10. pointsArray[1] = endPoint;
  11.  
  12. // Erase polyline and polyline view if not nil.
  13. if (self.routeLine != nil) {
  14. [_routeLine release];
  15. self.routeLine = nil;
  16. }
  17.  
  18. if (self.routeLineView != nil) {
  19. [_routeLineView release];
  20. self.routeLineView = nil;
  21. }
  22.  
  23. // Create the polyline based on the array of points.
  24. self.routeLine = [MKPolyline polylineWithPoints:pointsArray count:2];
  25.  
  26. // Add overlay to map.
  27. [self.mapView addOverlay:self.routeLine];
  28.  
  29. // clear the memory allocated earlier for the points.
  30. free(pointsArray);
  31.  
  32. // Save old coordinates.
  33. oldLatitude = newLatitude;
  34. oldLongitude = newLongitude;

所以我释放了以前的叠加层的routeLine对象.所以当我试图删除它,它崩溃,因为它已被释放.

以下是用于添加重叠式视图的mapView代理的代码

  1. - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
  2. {
  3. MKOverlayView* overlayView = nil;
  4.  
  5. if(overlay == _routeLine) {
  6. // If we have not yet created an overlay view for this overlay,create it now.
  7. if(self.routeLineView == nil) {
  8. self.routeLineView = [[[MKPolylineView alloc] initWithPolyline:_routeLine] autorelease];
  9.  
  10. self.routeLineView.fillColor = [UIColor blueColor];
  11. self.routeLineView.strokeColor = [UIColor blueColor];
  12.  
  13. // Size of the trace.
  14. self.routeLineView.lineWidth = routeLineWidth;
  15. }
  16.  
  17. overlayView = self.routeLineView;
  18. }
  19.  
  20. return overlayView;
  21. }

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

更新2

我试图没有释放我的routeLine和routeLineView对象,它现在工作.似乎也没有泄漏.所以现在我这样做:

  1. // Erase polyline and polyline view if not nil.
  2. if (self.routeLine != nil) {
  3. //[_routeLine release];
  4. self.routeLine = nil;
  5. }
  6.  
  7. if (self.routeLineView != nil) {
  8. //[_routeLineView release];
  9. self.routeLineView = nil;
  10. }

解决方法

当您调用removeOverlays:时,地图视图将释放MKOverlay和MKOverlayView对象.

您在_routeLine和_routeLineView中持有您自己的引用.

removeOverlays:被调用后,变量将指向已经释放的内存.当您重新创建折线时,您将被过度释放,从而导致崩溃.

所以删除释放调用

  1. if (_routeLine != nil) {
  2. [_routeLine release]; // <-- remove this
  3. self.routeLine = nil;
  4. }
  5.  
  6. if (_routeLineView != nil) {
  7. [_routeLineView release]; // <-- remove this
  8. self.routeLineView = nil;
  9. }

猜你在找的iOS相关文章