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

时光毁灭记忆、已成空白 提交于 2019-11-28 12:53:51

问题


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:


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


来源:https://stackoverflow.com/questions/31682999/for-google-maps-ios-sdk-current-location-occasionally-gets-plotted-at-0-0

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