MKMapView 因 setRegion 而崩溃

Posted

技术标签:

【中文标题】MKMapView 因 setRegion 而崩溃【英文标题】:MKMapView crashes by setRegion 【发布时间】:2013-02-08 11:41:58 【问题描述】:

我想在地图上显示一个特定的地址。这是我的代码。

- (void)viewDidLoad
[super viewDidLoad];

CLLocationCoordinate2D startCoord = CLLocationCoordinate2DMake(51.514302, 7.616798);
MKCoordinateRegion adjustedRegion = [_map regionThatFits:MKCoordinateRegionMakeWithDistance(startCoord, 200.0, 200.0)];
adjustedRegion.span = MKCoordinateSpanMake(0.005, 0.005);

[_map setRegion:adjustedRegion];

在模拟器上它工作正常,但在我的 iPad 上它崩溃了...... 我的错在哪里?

Last Exception Backtrace:
0   CoreFoundation                  0x33cbe3e2 __exceptionPreprocess + 158
1   libobjc.A.dylib                 0x3bb4e95e objc_exception_throw + 26
2   CoreFoundation                  0x33cbe0d0 -[NSException raise] + 4
3   MapKit                          0x34aec530 -[MKMapView setRegion:animated:] + 672
4   MapKit                          0x34aec64a __32-[MKMapView setRegion:animated:]_block_invoke_0 + 90
5   MapKit                          0x34ad6006 -[MKMapView _updateCenteredAttributesWithCoordinate:] + 122
6   MapKit                          0x34ad5f3a -[MKMapView _sizeDidChangeWithCenterCoordinate:] + 126
7   MapKit                          0x34ae8b58 -[MKMapView setBounds:] + 224
8   UIKit                           0x35cb2476 -[UIView(Geometry) _applyISEngineLayoutValues] + 246
9   UIKit                           0x35b3068a -[UIView(Geometry) _resizeWithOldSuperviewSize:] + 138
10  CoreFoundation                  0x33cb9c1c __NSArrayChunkIterate + 364
11  CoreFoundation                  0x33cb2bb2 __NSArrayEnumerate + 590
12  CoreFoundation                  0x33c14e32 -[NSArray enumerateObjectsWithOptions:usingBlock:] + 58
13  UIKit                           0x35ae9096 -[UIView(Geometry) resizeSubviewsWithOldSize:] + 114
14  UIKit                           0x35f09da8 -[UIView(AdditionalLayoutSupport) _is_layout] + 112
15  UIKit                           0x35ad6ae0 -[UIView(Hierarchy) layoutSubviews] + 68
16  UIKit                           0x35ad08c2 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 254
17  QuartzCore                      0x3587c50e -[CALayer layoutSublayers] + 210
18  QuartzCore                      0x3587c0b0 CA::Layer::layout_if_needed(CA::Transaction*) + 456
19  QuartzCore                      0x358aaeca -[CALayer layoutIfNeeded] + 138
20  UIKit                           0x35b7a0dc -[UIViewController window:setupWithInterfaceOrientation:] + 204
21  UIKit                           0x35b792c6 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 3598
22  UIKit                           0x35b784aa -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 42
23  UIKit                           0x35b78434 -[UIWindow _setRotatableViewOrientation:duration:force:] + 64
24  UIKit                           0x35cadcb4 __57-[UIWindow _updateToInterfaceOrientation:duration:force:]_block_invoke_0 + 100
25  UIKit                           0x35b3667e -[UIWindow _updateToInterfaceOrientation:duration:force:] + 214
26  UIKit                           0x35b363c2 -[UIWindow setAutorotates:forceUpdateInterfaceOrientation:] + 690
27  UIKit                           0x35b35d2a -[UIViewController _tryBecomeRootViewControllerInWindow:] + 150
28  UIKit                           0x35b2ce86 -[UIWindow addRootViewControllerViewIfPossible] + 366
29  UIKit                           0x35b28af0 -[UIWindow _setHidden:forced:] + 360
30  UIKit                           0x35b6a1dc -[UIWindow makeKeyAndVisible] + 56
31  UIKit                           0x35b2d7fe -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1642
32  UIKit                           0x35b2582e -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 694
33  UIKit                           0x35acdd1a -[UIApplication handleEvent:withNewEvent:] + 1006
34  UIKit                           0x35acd7a8 -[UIApplication sendEvent:] + 68
35  UIKit                           0x35acd1ea _UIApplicationHandleEvent + 6194
36  GraphicsServices                0x377c05f2 _PurpleEventCallback + 586
37  GraphicsServices                0x377c0222 PurpleEventCallback + 30
38  CoreFoundation                  0x33c933e2 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 30
39  CoreFoundation                  0x33c93386 __CFRunLoopDoSource1 + 134
40  CoreFoundation                  0x33c9220a __CFRunLoopRun + 1378
41  CoreFoundation                  0x33c05238 CFRunLoopRunSpecific + 352
42  CoreFoundation                  0x33c050c4 CFRunLoopRunInMode + 100
43  UIKit                           0x35b24468 -[UIApplication _run] + 664
44  UIKit                           0x35b212b4 UIApplicationMain + 1116
45  AD HOK                          0x000bcd80 main (main.m:16)
46  libdyld.dylib                   0x3bf7bb1c start + 0


Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0   libsystem_kernel.dylib          0x3c042350 __pthread_kill + 8
1   libsystem_c.dylib               0x3bfb8fb2 pthread_kill + 54
2   libsystem_c.dylib               0x3bff5366 abort + 90
3   libc++abi.dylib                 0x3b59ddda abort_message + 70
4   libc++abi.dylib                 0x3b59b094 _ZL17default_terminatev + 20
5   libobjc.A.dylib                 0x3bb4ea58 _ZL15_objc_terminatev + 144
6   libc++abi.dylib                 0x3b59b118 _ZL19safe_handler_callerPFvvE + 76
7   libc++abi.dylib                 0x3b59b1b0 std::terminate() + 16
8   libc++abi.dylib                 0x3b59c626 __cxa_rethrow + 90
9   libobjc.A.dylib                 0x3bb4e9b0 objc_exception_rethrow + 8
10  CoreFoundation                  0x33c0529c CFRunLoopRunSpecific + 452
11  CoreFoundation                  0x33c050c4 CFRunLoopRunInMode + 100
12  UIKit                           0x35b24468 -[UIApplication _run] + 664
13  UIKit                           0x35b212b4 UIApplicationMain + 1116
14  AD HOK                          0x000bcd80 main (main.m:16)
15  libdyld.dylib                   0x3bf7bb1c start + 0

Thread 1:
0   libsystem_kernel.dylib          0x3c042d98 __workq_kernreturn + 8
1   libsystem_c.dylib               0x3bf90ad6 _pthread_workq_return + 14
2   libsystem_c.dylib               0x3bf907f2 _pthread_wqthread + 362
3   libsystem_c.dylib               0x3bf90680 start_wqthread + 4

Thread 2 name:  Dispatch queue: com.apple.libdispatch-manager
Thread 2:
0   libsystem_kernel.dylib          0x3c0325d0 kevent64 + 24
1   libdispatch.dylib               0x3bf6dd22 _dispatch_mgr_invoke + 806
2   libdispatch.dylib               0x3bf69374 _dispatch_mgr_thread + 32

Thread 3:
0   libsystem_kernel.dylib          0x3c042d98 __workq_kernreturn + 8
1   libsystem_c.dylib               0x3bf90ad6 _pthread_workq_return + 14
2   libsystem_c.dylib               0x3bf907f2 _pthread_wqthread + 362
3   libsystem_c.dylib               0x3bf90680 start_wqthread + 4

Thread 4 name:  WebThread
Thread 4:
0   libsystem_kernel.dylib          0x3c031e30 mach_msg_trap + 20
1   libsystem_kernel.dylib          0x3c031fd0 mach_msg + 48
2   CoreFoundation                  0x33c932b6 __CFRunLoopServiceMachPort + 126
3   CoreFoundation                  0x33c9202c __CFRunLoopRun + 900
4   CoreFoundation                  0x33c05238 CFRunLoopRunSpecific + 352
5   CoreFoundation                  0x33c050c4 CFRunLoopRunInMode + 100
6   WebCore                         0x39be0390 _ZL12RunWebThreadPv + 440
7   libsystem_c.dylib               0x3bf9b0de _pthread_start + 306
8   libsystem_c.dylib               0x3bf9afa4 thread_start + 4

Thread 5:
0   libsystem_kernel.dylib          0x3c042d98 __workq_kernreturn + 8
1   libsystem_c.dylib               0x3bf90ad6 _pthread_workq_return + 14
2   libsystem_c.dylib               0x3bf907f2 _pthread_wqthread + 362
3   libsystem_c.dylib               0x3bf90680 start_wqthread + 4

Thread 0 crashed with ARM Thread State (32-bit):
    r0: 0x00000000    r1: 0x00000000      r2: 0x00000000      r3: 0x3db31524
    r4: 0x00000006    r5: 0x3db31b78      r6: 0x210aa354      r7: 0x2fd459d4
    r8: 0x210aa330    r9: 0x00000300     r10: 0x00000000     r11: 0x7ff80000
    ip: 0x00000148    sp: 0x2fd459c8      lr: 0x3bfb8fb7      pc: 0x3c042350
  cpsr: 0x00000010

【问题讨论】:

设备上的确切错误消息是什么?您在模拟器中测试的 ios 版本是什么,设备是什么版本? 异常类型:EXC_CRASH (SIGABRT) 异常代码:0x0000000000000000, 0x0000000000000000 线程崩溃:0 i 在设备 6.1 和模拟器 6.1 上使用。 您可以发布堆栈跟踪(编辑您的问题)吗?如果可能,请在设备上运行调试器时单步调试代码。你确定它是在这段代码上崩溃而不是在其他地方吗? 好的,我已经在问题中添加了完整的堆栈跟踪。我无法在设备上调试抱歉...是的。当我写 //[_map setRegion:adjustedRegion];比它有效 【参考方案1】:

当您最终再次明确设置该区域的跨度时,不清楚您为什么要向地图询问适合的区域。此外,在布局完成之前,地图对这个问题没有有意义的答案。这可能是它崩溃的原因,因为该区域没有正确定义。

从您的代码看来,您只是想这样做:

- (void)viewDidLoad
   [super viewDidLoad];

   CLLocationCoordinate2D startCoord = CLLocationCoordinate2DMake(51.514302, 7.616798);
   [_map setRegion:MKCoordinateRegionMakeWithDistance(startCoord, 200.0, 200.0) animated:NO];

我认为这要简单得多,并且应该可以在设备上正常工作。希望对您有所帮助。

【讨论】:

以上是关于MKMapView 因 setRegion 而崩溃的主要内容,如果未能解决你的问题,请参考以下文章

SwiftUI 上的按钮点击调用 MKMapView.setRegion

将 MkMapView 移动到 TableViewCell 后,SetRegion 停止工作

MKMapView 和 setRegion:animated: 不更新地图视觉效果

MKMapView setRegion 到 Annotation

使用无效参数调用“setRegion”时,iOS 6 Map 崩溃

MKMapView 缩放和区域