向MKMapView添加MKPolyline覆盖层会导致应用程序崩溃。

6

我的应用在iOS6上运行良好,但是在iOS7上添加MKMapView覆盖层时会导致崩溃,提示Bad Access。以下是我的代码:

MKPolyline *polyline = [[MKPolyline alloc] init];
   polyline = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
    [directionMap addOverlay:polyline];

enter image description here

这是崩溃日志 (lldb) bt *

 thread #51: tid = 0x1cc5, 0x38755f8c libdispatch.dylib`dispatch_retain$VARIANT$mp + 8, stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
    frame #0: 0x38755f8c libdispatch.dylib`dispatch_retain$VARIANT$mp + 8
    frame #1: 0x3598dbc8 VectorKit`-[VKRasterOverlayTileSource init] + 176
    frame #2: 0x358cfd24 VectorKit`-[VKMapModel _rasterOverlayTileSourceForLevel:] + 308
    frame #3: 0x358d0226 VectorKit`-[VKMapModel addRasterOverlay:] + 46
    frame #4: 0x2f068dfe MapKit`-[MKOverlayContainerView _insertDrawable:forOverlay:atIndex:level:] + 1010
    frame #5: 0x2f06752e MapKit`-[MKOverlayContainerView _configureAndAddDrawable:forOverlay:level:] + 326
    frame #6: 0x2f0676ac MapKit`-[MKOverlayContainerView _considerAddingDrawable:inAddRect:level:] + 372
    frame #7: 0x2f067cce MapKit`-[MKOverlayContainerView addOverlay:level:] + 246
    frame #8: 0x001394c8 Falcon`-[GetDirectionVC showRouteFrom:to:](self=0x19742820, _cmd=0x001fa466, f=CLLocationCoordinate2D at 0x04f9ec2c, t=CLLocationCoordinate2D at 0x04f9ec1c) + 956 at GetDirectionVC.m:226
    frame #9: 0x001390ee Falcon`-[GetDirectionVC loadLocations](self=0x19742820, _cmd=0x001fa458) + 1314 at GetDirectionVC.m:173
    frame #10: 0x2e876e26 Foundation`__NSThread__main__ + 1062
    frame #11: 0x38891c1c libsystem_pthread.dylib`_pthread_body + 140
    frame #12: 0x38891b8e libsystem_pthread.dylib`_pthread_start + 102

(lldb)


请发布错误日志。 - Puneet Sharma
在文档中,MKPolyline 没有任何更改,但是 viewForOverlay 方法已经在 iOS7.0 中被弃用。请尝试使用 - (MKOverlayRenderer *)rendererForOverlay:(id<MKOverlay>)overlay 代替 viewForOverlay 方法。 - Puneet Sharma
你为什么要对MKPolyline对象进行alloc/init操作?polylineWithCoordinates方法返回一个MKPolyline对象。 - Puneet Sharma
我也是,我的代码在iOS 7之前都能正常工作,我已经为这个bug努力了好几个小时,看起来像是苹果的问题。 - Niralp
我从早上开始就一直在处理它,不知道为什么在addOverlay时会崩溃并出现错误访问。如果你找到了任何解决方法,请在这里分享。 - Mobile Developer iOS Android
显示剩余6条评论
2个回答

18

我有同样的问题,堆栈跟踪对我来说看起来很误导。我的错误修复是在主线程上显式添加覆盖层:

dispatch_async(dispatch_get_main_queue(), ^{
  [mapView addOverlay:myRouteLine];
});

或者如果您想使用新的MKOverlayRenderer:

dispatch_async(dispatch_get_main_queue(), ^{
  [mapView addOverlay:myRouteLine level:MKOverlayLevelAboveRoads];
});

在我的情况下,我异步下载一些数据,生成折线,创建MKOverlayViews/MKOverlayRenderes(替换过时代码没有帮助),并将该覆盖层添加到地图中。


1
谢谢!我已经寻找解决方案好几天了,就像你所说的,在iOS 7中该方法会在后台线程崩溃。 - Niralp
太棒了!谢谢Roger。 - dijipiji

1

如果您正在非主线程中创建折线,则使用以下内容:

[self performSelectorOnMainThread:@selector(addPolyLineToMap:) withObject:polyline waitUntilDone:NO];

-(void)addPolyLineToMap:(MKPolyline*)apolyline{
    [mapview addOverlay:apolyline];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接