【问题标题】:Adding MKPolyline Overlay to MKMapView crashes application将 MKPolyline Overlay 添加到 MKMapView 会导致应用程序崩溃
【发布时间】:2013-09-28 03:59:28
【问题描述】:

我的应用程序在 iOS6 上运行良好,但是当我将叠加层添加到 MKMapView 时,由于访问不正确,它在 iOS 7 上崩溃。我的代码如下

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

这是崩溃日志 (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)

【问题讨论】:

  • 请发布错误日志。
  • 我已经用崩溃日志编辑了问题,请检查一下。谢谢
  • 好吧,在文档中,MKpolyline 没有任何变化,但 viewForOverlay 方法在 iOS7.0 中已被弃用。尝试使用 - (MKOverlayRenderer *)rendererForOverlay:(id )overlay 代替 viewForOverlay 方法。
  • 是的,我正在使用新的 iOS 7 MKMapviewDelegate - (MKOverlayRenderer *) 但我不知道为什么我的访问崩溃了:(
  • 你为什么要为 MKpolyline 对象做 alloc/init? polylineWithCoordinates 方法返回一个 MKPolyline 对象。

标签: iphone ios objective-c mkmapview core-location


【解决方案1】:

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

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

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

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

就我而言,我正在异步下载一些数据,生成折线,创建 MKOverlayViews / MKOverlayRenderes(没有帮助替换已弃用的代码)并将叠加层添加到地图中。

【讨论】:

  • 谢谢!!正如您所说,我一直在寻找解决方案几天,该方法在 iOS 7 的后台线程上崩溃。
  • 太棒了!谢谢罗杰
【解决方案2】:

如果您在主线程以外的线程中创建折线,请使用以下内容:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多