对于Google地图iOS sdk,当前位置偶尔会被绘制为0,0 [英] For google maps iOS sdk, current location occasionally gets plotted at 0,0

查看:102
本文介绍了对于Google地图iOS sdk,当前位置偶尔会被绘制为0,0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道为什么会发生这种情况,但是当它发生时,我们不希望向用户显示他们当前的位置是0,0(位于南非海岸外的海洋中间)。有没有什么办法可以检测到这种情况发生,所以我们可以向用户显示错误信息或者其他信息?解析方案

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

  NSLocationAlwaysUsageDescription 
NSLocationWhenInUseUsageDescription

现在,当您完成此操作后,您将不得不请求授权,如下所示:

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

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

  [locationManager startUpdatingLocation]; 

现在如果你想要去当前的位置,你需要在这里实现这个方法: (CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation * location = [

$ pre $ - (void)locationManager [位置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如果([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied)
{
if $ {
//将您的警报放在这里。
}
}
}


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

这篇关于对于Google地图iOS sdk,当前位置偶尔会被绘制为0,0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆