简体   繁体   中英

For google maps iOS sdk, current location occasionally gets plotted at 0,0

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?

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.
        }
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM