在iPhone上的GoogleMaps上获取我的位置时崩溃 [英] Crash while getting my location on GoogleMaps on iPhone
问题描述
我写信给你是因为我被一个超级奇怪的错误困住了. 我试图在我的应用程序上添加GMS,最后,当我必须获取设备位置时,我陷入了此崩溃:
I am writing you because I am stuck with a super strange error. I tried to add GMS on my app, and finally, when I have to get device location, I am stuck with this crash:
由于未捕获的异常"NSInternalInconsistencyException"而终止应用程序,原因:已释放分配了GMSMapView类的实例0x7ff7fd82def0,同时仍向其注册了键值观察器.当前观察信息:( 内容:0x0,属性:0x7ff7fdc2f470>
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'An instance 0x7ff7fd82def0 of class GMSMapView was deallocated while key value observers were still registered with it. Current observation info: ( Context: 0x0, Property: 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方法中,一旦离开这里,就会出现崩溃.没办法.
and still no success. I put breakpoint in observerValueForKey method, and once is out from here, crash appears. No way to get the idea.
我还删除了观察者:
- (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;
}
没有成功.
有人可以帮助我吗?我尝试了所有可能的解决方案,但是没有办法.
Can anyone help me with this ? I tried all possible solutions, but no way.
提前谢谢!
推荐答案
在评论中已经说过了.GMSMapView必须为Strong
如果您不调用requestWhenInUseAuthorization
How already said in comment.. the GMSMapView must be Strong
Your code will not work in iOS 8 if you not call requestWhenInUseAuthorization
我建议您仅使用CLLocationManager而不是KVO ...,但这是您的决定.
I recommend you to use only the CLLocationManager instead KVO... but is your decision.
附上我的示例 -我修改了Google示例...有2个示例:
Attached my example - I modified the Google example... with 2 example:
- 使用KVO
- 仅适用于CLLocationManager
如果有任何问题,请告诉我
If you have any issues... just let me know
PS别忘了添加您的API密钥
PS Don't forget to add you API Key
祝您编程愉快!
更新
如果您启动了UpUpdatingLocation ..请不要忘记使用stopUpdatingLocation
将其停止..在ViewDidDisapear中说
if you startUpdatingLocation.. don't forget to stop it somewhere with stopUpdatingLocation
.. let say in ViewDidDisapear
这篇关于在iPhone上的GoogleMaps上获取我的位置时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!