1

Not sure why this happens but when it does we'd prefer not to show the user that their current location is 0,0 (it's in the middle of the ocean off the coast of South Africa). Is there any way to detect when this happens so we can show an error message or something to the user?

Harry Jiang
  • 721
  • 6
  • 16
  • Did you tried in that in your simulator or actual device? If in an actual device, you need to simulate a location. – ztan Jul 28 '15 at 21:38

1 Answers1

4

Looks like you are not authorized to access the current location of the device. For that, you'll have to add these two keys into your plist:

 NSLocationAlwaysUsageDescription
 NSLocationWhenInUseUsageDescription

Now when you're done with with it, you will have to request for authorization like this :

if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
{
    [locationManager requestWhenInUseAuthorization];
}

So now when you are done with authorization, you need to start getting location updates. For this, use this line :

[locationManager startUpdatingLocation];

Now if you wish to go to the current location, you need to implement this method here:

 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
               CLLocation *location = [locations lastObject];
                GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:location.coordinate.latitude longitude:location.coordinate.longitude zoom:currentZoom];                                                                   
    [self.gMapView animateToCameraPosition:camera];
}

This will keep updating the map with current location. If you just want to current location once, just put a conditional statement for the same.

If user denies the permission, following method will be called :

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
if([CLLocationManager locationServicesEnabled])
{
    if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied)
    {
     // Put your alert here.
        }
    }
  }
Bharat
  • 340
  • 1
  • 8
  • it's necessary to use GMSCameraPosition to get location ? I just want to get location without Internet connection .. – kiamoz Jul 01 '16 at 15:37
  • No. You don't need to use that. Just use this line `CLLocation *location = [locations lastObject];` – Bharat Jul 01 '16 at 19:39