【问题标题】:IOS 8 Map is not working: Trying to start MapKit location updates without prompting for location authorizationIOS 8 Map 不工作:尝试在不提示位置授权的情况下启动 MapKit 位置更新
【发布时间】:2014-12-10 03:08:57
【问题描述】:

我正在使用 Xcode 6。当我尝试使用 CLLocationManager 获取用户当前位置时遇到一些问题。甚至我将NSLocationWhenInUseUsageDescriptionNSLocationAlwaysUsageDescription 添加到Info.plist 文件中。并且还使用了[CLLocationManager requestWhenInUseAuthorization]。然后我也得到控制台输出为

2014-10-15 11:45:15.004 MapIOS8[1916:57908] 尝试在不提示位置授权的情况下启动 MapKit 位置更新。必须先调用 -[CLLocationManager requestWhenInUseAuthorization] 或 -[CLLocationManager requestAlwaysAuthorization]。

【问题讨论】:

  • 所以你想要的只是获取用户的当前位置?
  • 您显然是在调用 requestWhenInUse 来迟到...显示您的代码。
  • 我想要的是,我想在地图上显示用户当前位置
  • - (void)viewDidLoad { [super viewDidLoad]; CLLocationManager *myManager=[[CLLocationManager alloc]init]; myManager.delegate=自己; if([CLLocationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { NSLog(@"Inside This method"); [myManager requestWhenInUseAuthorization]; } MKCoordinateRegion 我的区域; myregion.span.latitudeDelta=0.11; myregion.span.longitudeDelta=0.11; [myMap setRegion:myregion Animation:YES]; }
  • 在您成功回调您的 CLLocationManagerDelegate 委托之前,您不应启用“显示当前位置”属性

标签: ios mapkit


【解决方案1】:

您是否尝试过按此顺序编写代码?

CLLocationManager *YourLocationManager = [[CLLocationManager alloc] init];
YourLocationManager.delegate = self;
[YourLocationManager requestWhenInUseAuthorization];
[YourLocationManager startUpdatingLocation];

yourMapView.delegate = self;
yourMapView.showsUserLocation = YES;

另外,要获取用户位置坐标,您可能必须实现这个 mapkit 委托方法

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    NSLog(@"%f, %f", userLocation.coordinate.latitude, userLocation.coordinate.longitude);

    [mapView selectAnnotation:userLocation animated:YES];
}

【讨论】:

    【解决方案2】:

    如果您使用的是 iOS 模拟器,请尝试从“设置”->“常规”->“隐私”->“重置位置和隐私”中重置位置设置。然后调用:

    [LocationManager requestAlwaysAuthorization];
    

    在第一次执行时必须出现一个弹出窗口以允许使用当前用户的位置。允许使用当前位置后,使用以下命令启动您的 LocationManager:

    [LocationManager startUpdatingLocation];
    

    【讨论】:

      猜你喜欢
      • 2015-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-02
      • 2014-11-17
      • 1970-01-01
      • 1970-01-01
      • 2014-01-28
      相关资源
      最近更新 更多