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

自作多情 提交于 2019-11-29 18:07:07

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