【发布时间】:2014-11-28 18:05:17
【问题描述】:
我给你写信是因为我遇到了一个非常奇怪的错误。 我试图在我的应用程序上添加 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.. 像这样@property (nonatomic, strong) GMSMapView *mapView? -
我将它设置为 WEAK,因为它是 IBOutlet @property (weak, nonatomic) IBOutlet GMSMapView *mapView;
-
@TonyMkenu 是的...我将其更改为 '@property (strong) IBOutlet GMSMapView *mapView;'现在没有崩溃......但地图不可见。 1秒后隐藏。现在不调用“observeValueForKeyPath:”:?
-
那么,你的 GMSMap 有一些问题,可能现在它没有正确连接到你的 IBOutlet
-
@TonyMkenu 我刚刚添加了一个 UIView,然后子类化为 GMSMapView,然后连接了 IBOutlet。没有别的了。
标签: ios objective-c xcode google-maps-sdk-ios gmsmapview