ios - 在 iPhone 上的 Google map 上获取我的位置时崩溃

标签 ios objective-c xcode google-maps-sdk-ios gmsmapview

我写信给你是因为我遇到了一个 super 奇怪的错误。 我尝试在我的应用程序上添加 GMS,最后,当我必须获取设备位置时,我遇到了这次崩溃:

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“类 GMSMapView 的实例 0x7ff7fd82def0 已被释放,而键值观察者仍向其注册。当前观测信息:( 上下文:0x0,属性:0x7ff7fdc2f470>

代码在这里初始化:

self.mapView.myLocationEnabled = YES;
self.mapView.mapType = kGMSTypeNormal;
self.mapView.settings.compassButton = YES;
self.mapView.settings.myLocationButton = YES;

//=>    Listen to the myLocation property of GMSMapView.
[self.mapView addObserver:self
               forKeyPath:@"myLocation"
                  options:NSKeyValueObservingOptionNew
                  context:NULL];


- (void)observeValueForKeyPath:(NSString *)keyPath
                  ofObject:(id)object
                    change:(NSDictionary *)change
                   context:(void *)context
{
if ([keyPath isEqualToString:@"myLocation"] && [object isKindOfClass:[GMSMapView class]])
{   
    appDelegate().curUserLocation = [change objectForKey:NSKeyValueChangeNewKey];

    [self.mapView animateToCameraPosition:[GMSCameraPosition cameraWithLatitude:self.mapView.myLocation.coordinate.latitude
                                                                             longitude:self.mapView.myLocation.coordinate.longitude
                                                                                  zoom:15]];
}
}

仍然没有成功。我在 observerValueForKey 方法中放置了断点,一旦从这里退出,就会出现崩溃。无法理解。

我还删除了观察者:

- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];

[self.mapView removeObserver:self forKeyPath:@"myLocation"];

//=>    Set map delegate to nil (to avoid:   mapView:regionDidChangeAnimated:]: message sent to deallocated instance )
self.mapView.delegate = nil;
self.mapView = nil;
}

没有成功。

谁能帮我解决这个问题?我尝试了所有可能的解决方案,但没有办法。

提前致谢!

最佳答案

评论中已经说过了.. GMSMapView 必须是 Strong 如果您不调用 requestWhenInUseAuthorization

,您的代码将无法在 iOS 8 中运行

我建议您仅使用 CLLocationManager 而不是 KVO...但您可以自行决定。

Attached my example - 我修改了谷歌示例......有 2 个示例:

  1. 与 KVO
  2. 仅使用 CLLocationManager

如果你有任何问题......请告诉我

PS 不要忘记添加 API Key

编码愉快!

更新

如果你 startUpdatingLocation.. 不要忘记用 stopUpdatingLocation 停止它.. 假设在 ViewDidDisapear

关于ios - 在 iPhone 上的 Google map 上获取我的位置时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27193946/

相关文章:

objective-c - 将 C 数据类型转换为 objective-c 对象

iphone - UIButton 导致 UITextField 字删除

xcode - 管理 Xcode 工作区

ios - XCODE swift - webview 无法打开 ics 链接 - 在 safari 中它有效

ios - AVCaptureVideoPreviewLayer 未显示预览中心

objective-c - 如何通过 CGRect 过滤 NSArray 中的 CGPoints

iOS - 如何创建带阴影的 "material design"兼容 UIView?

xcode - Swift 中的字符串问题

ios - 如何搜索 PFObject UITableView

ios - MapKit Step折线在哪里?