GMSMapView myLocation does not give actual location

I have GMSMapView loaded correctly and working inside my view manager

what I can not do is set GMSCameraPosition around my location

this is my code:

mapView_.myLocationEnabled = YES; CLLocation* myLoc = [mapView_ myLocation]; GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:myLoc.coordinate.latitude longitude:myLoc.coordinate.longitude zoom:4]; [mapView_ setCamera:camera]; 

GPS is turned on and the application has all the necessary permissions, but myLocation returns zero CLLocation, therefore cameraWithLatitude:longitude:zoom: gets 0 0 coordinates and displays Africa instead of my actual location (this is not in Africa :))

+5
source share
2 answers

With official Google Maps iOS SDK documentation:

  • (BOOL) myLocationEnabled [read, write, assign] Controls whether the My Location and Precision points are enabled.

The default is NO.

  • (CLLocation *) myLocation [read, assign] If My Location is enabled, displays where the user's location point is displayed.

If it is disabled or enabled, but location data is not available, it will be nil. This property can be observed using KVO.

Therefore, when you set mapView_.myLocationEnabled = YES; , it tells mapView to show the blue dot only if you have the location value assigned to the myLocation property. A sample code from Google shows how to monitor a user's location using the KVO method. (Recommended) You can also implement the CLLocationManagerDelegate method to update mapView.

 -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { [mapView animateToLocation:newLocation.coordinate]; // some code... } 

Here is the code from the Google Maps sample code that describes how to use KVO to update the user's location.

 // in viewDidLoad method... // Listen to the myLocation property of GMSMapView. [mapView_ addObserver:self forKeyPath:@"myLocation" options:NSKeyValueObservingOptionNew context:NULL]; // Ask for My Location data after the map has already been added to the UI. dispatch_async(dispatch_get_main_queue(), ^{ mapView_.myLocationEnabled = YES; }); - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if (!firstLocationUpdate_) { // If the first location update has not yet been recieved, then jump to that // location. firstLocationUpdate_ = YES; CLLocation *location = [change objectForKey:NSKeyValueChangeNewKey]; mapView_.camera = [GMSCameraPosition cameraWithTarget:location.coordinate zoom:14]; } } 
+12
source

Try using MapKit under VieDidLoad or ViewWillApper using:

 myMapView.showsUserLocation = YES; 

You can add this simple code under IBAction if you want, for example:

 - (IBAction)getLocation:(id)sender{ myMapView.showsUserLocation = YES; } 

I hope this helps you

-4
source

All Articles