我写信给你是因为我遇到了一个 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
我建议您仅使用 CLLocationManager 而不是 KVO...但您可以自行决定。
Attached my example - 我修改了谷歌示例......有 2 个示例:
- 与 KVO
- 仅使用 CLLocationManager
如果你有任何问题......请告诉我
PS 不要忘记添加 API Key
编码愉快!
更新
如果你 startUpdatingLocation.. 不要忘记用 stopUpdatingLocation
停止它.. 假设在 ViewDidDisapear
关于ios - 在 iPhone 上的 Google map 上获取我的位置时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27193946/