IOS定位,获取经纬度,IOS8环境
2015-06-02 11:21
351 查看
// // FLShopsCtl.m // eher365 // // Created by fulin on 15/6/1. // Copyright (c) 2015年 fulin. All rights reserved. // /***************************** *****************************/ #import <CoreLocation/CoreLocation.h> #import "FLShopsCtl.h" #import "FLShopsTableViewCell.h" #import "FLSearchShopsCtl.h" #import "FLShopsDetailCtl.h" @interface FLShopsCtl () <CLLocationManagerDelegate> - (IBAction)clickWithSearchView; @property(nonatomic, strong) NSString* currentLatitude; @property(nonatomic, strong) NSString* currentLongitude; @property(nonatomic, strong) CLLocationManager *locationManager; @end @implementation FLShopsCtl static NSString * const reuseIdentifier = @"shops_id"; - (void)viewDidLoad { //定位 [self setupMapGetAddress]; } //------------------------------------------------------------------------------------------------------------ //定位 - (void)setupMapGetAddress { CLLocationManager *locManager = [[CLLocationManager alloc] init]; locManager.delegate = self; locManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; locManager.distanceFilter = 5.0; if ([[[UIDevice currentDevice] systemVersion] doubleValue] > 8.0) { [locManager requestWhenInUseAuthorization]; [locManager requestAlwaysAuthorization]; } [locManager startUpdatingLocation]; self.locationManager = locManager; } #pragma mark - CLLocationManagerDelegate 定位成功 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { [self.locationManager stopUpdatingLocation]; CLLocation *newLocation = [locations objectAtIndex:0]; NSString *strLat = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.latitude]; NSString *strLng = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.longitude]; NSLog(@"Lat: %@ Lng: %@", strLat, strLng); } #pragma mark - CLLocationManagerDelegate 定位失败 - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { [self.locationManager stopUpdatingLocation]; //定位失败, 找到原因,并且处理 } //------------------------------------------------------------------------------------------------------------ @end
补充:
iOS8中定位服务的变化(CLLocationManager协议方法不响应,无法回掉GPS方法,不出现获取权限提示)
以下内容转载:最近在写一个LBS的项目的时候,因为考虑到适配iOS8,就将项目迁移到Xcode6.0.1上,出现了不能正常获取定位服务权限的问题。
self.manger = [[CLLocationManager alloc]init]; self.manger.distanceFilter = kCLDistanceFilterNone; // meters self.manger.delegate = self; [self.manger requestAlwaysAuthorization]; self.manger.desiredAccuracy = kCLLocationAccuracyBestForNavigation; [self.manger startUpdatingLocation];
以上是iOS8之前的调用方法,当用户开启App的时候,会主动询问是否允许开启定位服务权限,现在在iOS8中,无论是模拟器还是真机,都不进行提示,经过一番查阅官方文档-->LocationAwarenessPGIntroduction 发现,Apple在iOS8中加强了隐私访问权限的控制,必须调用新的方法来获取用户的允许
[self.manager requestWhenInUseAuthorization];
并且在info.plist文件中增加
NSLocationWhenInUseUsageDescription BOOL YES
NSLocationAlwaysUsageDescription string “提示描述”
两个字段,在iOS8中才能进行正确的获取服务权限
在使用高德和百度地图的时候,出现了编译错误,大致原因是因为Xcode6中默认为使用64编译造成了,如果解决,后期博文会进行更新
相关文章推荐
- ios开发storyboard的一些小技巧
- iOS开发中常用设计模式
- iOS 上传ipa后预发行 二进制无效
- iOS设备上回声消除的例子
- iOS中NSCharacterSet的使用详解
- ios基础之 透过页面跳转来认识 Strong 与 Weak
- ios页面间传递参数四种方式
- iOS 开发怎么入门?
- IOS8 地图设置
- JS与iOS之间的通信
- iOS简单的动画效果实现
- iOS - 版面实现记录三
- iOS AFNetWorking上传文件
- 2014最新苹果开发者账号注册流程图解分享
- iOS - 版面实现记录二
- 安卓自定义日期控件(仿QQ,IOS7)续
- iOS - 版面实现记录一
- iPhone6+ 以及 iOS8适配:Masonry介绍——速成Autolayout
- iOS开源项目之日志框架CocoaLumberjack
- iOS使用矢量图的总结