Crash while getting my location on GoogleMaps on iPhone

谁说我不能喝 提交于 2019-12-23 21:31:13

问题


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:

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>

Code is initialized here:

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]];
}
}

and still no success. I put breakpoint in observerValueForKey method, and once is out from here, crash appears. No way to get the idea.

I also removed observer :

- (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;
}

and no success.

Can anyone help me with this ? I tried all possible solutions, but no way.

Thanks in advance!


回答1:


How already said in comment.. the GMSMapView must be Strong Your code will not work in iOS 8 if you not call requestWhenInUseAuthorization

I recommend you to use only the CLLocationManager instead KVO... but is your decision.

Attached my example - I modified the Google example... with 2 example:

  1. with KVO
  2. only with CLLocationManager

If you have any issues... just let me know

PS Don't forget to add you API Key

Happy coding!

UPDATE

if you startUpdatingLocation.. don't forget to stop it somewhere with stopUpdatingLocation.. let say in ViewDidDisapear




回答2:


If you register inside viewWillAppear:, use viewWillDisappear to remove observer.

If you register inside viewDidLoad, use deinit to unregister the observer. Always use the counter part to register and unregister and that should be fine

Swift 3

deinit {
     mapView.removeObserver(self, forKeyPath: "myLocation")
}


来源:https://stackoverflow.com/questions/27193946/crash-while-getting-my-location-on-googlemaps-on-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!