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

时间:2015-07-28 17:13:41

标签: ios google-maps google-maps-sdk-ios

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?

1 个答案:

答案 0 :(得分:3)

您似乎无权访问设备的当前位置。为此,您必须将这两个键添加到您的plist中:

 NSLocationAlwaysUsageDescription
 NSLocationWhenInUseUsageDescription

现在,当你完成它时,你必须要求这样的授权:

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

现在,当您完成授权后,您需要开始获取位置更新。为此,请使用以下行:

[locationManager startUpdatingLocation];

现在,如果您希望转到当前位置,则需要在此处实施此方法:

 - (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];
}

这将继续使用当前位置更新地图。 如果您只想要当前位置一次,只需为其设置一个条件语句。

如果用户拒绝该权限,将调用以下方法:

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
if([CLLocationManager locationServicesEnabled])
{
    if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied)
    {
     // Put your alert here.
        }
    }
  }