首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >iOS 8地图视图当前位置未触发

iOS 8地图视图当前位置未触发
EN

Stack Overflow用户
提问于 2014-09-18 19:21:42
回答 3查看 4.5K关注 0票数 5

MKMapview当前用户位置在iOS-8中未触发,以前的iOS-7iOS-6工作正常。

代码语言:javascript
运行
复制
     self.mapView.delegate = self;
     self.mapView.showsUserLocation =YES; 

在这一行中自动调用用户当前的位置委托方法

代码语言:javascript
运行
复制
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
}

但它不会在iOS-8中触发。

EN

回答 3

Stack Overflow用户

发布于 2014-09-20 06:40:42

在iOS8中,在获取用户位置之前,您需要请求用户的授权。

有两种类型的请求:

只有当应用程序被唤醒时,-[CLLocationManager requestWhenInUseAuthorization]才能让你获得用户的位置。

-[CLLocationManager requestAlwaysAuthorization]可以让你获得用户的位置,即使它是在后台。

您可以相应地在它们之间进行选择。

例如,在开始更新位置之前放入以下内容:

代码语言:javascript
运行
复制
// ask for authorization
CLLocationManager * locationManager = [[CLLocationManager alloc] init];
// check before requesting, otherwise it might crash in older version
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { 

     [locationManager requestWhenInUseAuthorization];

}

此外,不要忘记添加两个键

代码语言:javascript
运行
复制
NSLocationWhenInUseUsageDescription

代码语言:javascript
运行
复制
NSLocationAlwaysUsageDescription

添加到你的info.plist中。

将值保留为空以使用默认消息,或者您可以通过输入值来自定义您自己的消息。

票数 6
EN

Stack Overflow用户

发布于 2014-10-16 18:33:28

在ios 8中,需要进行如下授权

代码语言:javascript
运行
复制
    NSString * osVersion = [[UIDevice currentDevice] systemVersion];
    if ([osVersion floatValue]>= 8.0 ) {
    [_CLLocationManager requestAlwaysAuthorization]; //Requests permission to use location services whenever the app is running. 
// [_CLLocationManager requestWhenInUseAuthorization]; //Requests permission to use location services while the app is in the foreground. 
    }
    [_CLLocationManager startUpdatingLocation];

并且您需要在plist中添加两个键

1.NSLocationAlwaysUsageDescription

2.NSLocationWhenInUseUsageDescription

票数 3
EN

Stack Overflow用户

发布于 2014-10-08 13:48:33

在iOS 8 Mapview中,当前位置使用

您可以在.plist文件中设置

代码语言:javascript
运行
复制
<key>NSLocationWhenInUseUsageDescription</key>
<string>Your app name Or Other string</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Your app name Or Other string</string>

在你的代码端,你只需要实现这个

CLLocationManager * locationmanager = CLLocationManager新;

代码语言:javascript
运行
复制
locationmanager.delegate = self;
locationmanager.distanceFilter = kCLDistanceFilterNone;
locationmanager.desiredAccuracy = kCLLocationAccuracyBest;
[locationmanager startUpdatingLocation];    
[locationmanager requestAlwaysAuthorization];

-(空)locationManager:(CLLocationManager*)管理器didUpdateLocations:(NSArray *)位置{

代码语言:javascript
运行
复制
location = [locations objectAtIndex:0];
[locationmanager stopUpdatingLocation];

}

我正在使用我的应用程序获得完美的当前位置。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25911330

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档