Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GMSMapView myLocation not giving actual location

I have a GMSMapView properly loaded and working inside my viewcontroller

what i'm not being able to do is setting the 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 enabled and application has all needed permissions but myLocation returns a nil CLLocation, consequentially cameraWithLatitude:longitude:zoom: get 0 0 coordinates and displays Africa instead of my actual location (that is not in africa :) )

like image 345
Zerho Avatar asked Jun 28 '13 13:06

Zerho


1 Answers

From official Google Maps iOS SDK documentation:

  • (BOOL) myLocationEnabled [read, write, assign] Controls whether the My Location dot and accuracy circle is enabled.

Defaults to NO.

  • (CLLocation*) myLocation [read, assign] If My Location is enabled, reveals where the user location dot is being drawn.

If it is disabled, or it is enabled but no location data is available, this will be nil. This property is observable using KVO.

So when you set mapView_.myLocationEnabled = YES;, it only tells the mapView to reveal the blue dot only if you have a location value given to the myLocation property. The sample code from Google shows how to observe the user location using the KVO method.(Recommended) You can also implement the CLLocationManagerDelegate method, to update the mapView.

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

Here are the code from google maps sample code on how to use KVO to update user 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];
  }
}
like image 149
Jing Avatar answered Sep 19 '22 17:09

Jing