定位功能(使用系统地图)
2015-09-13 19:04
399 查看
1.导入MapKit,CoreLocation库
2.viewController文件
2.viewController文件
#import "ViewController.h" #import <CoreLocation/CoreLocation.h> @interface ViewController ()<CLLocationManagerDelegate> { CLLocationManager *_locationManager; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //1.创建locationManager _locationManager = [[CLLocationManager alloc] init]; //2.info.plist文件加入字段 //NSLocationWhenInUseDescription //NSLocationAlwaysUsageDescription if (![CLLocationManager locationServicesEnabled]) { NSLog(@"定位服务未打开"); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请打开定位服务" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alert show]; } //3.请求权限(一直允许访问位置信息) [_locationManager requestAlwaysAuthorization]; //程序运行期间允许访问位置信息 //[_locationManager requestWhenInUseAuthorization]; //4.定位精确度 _locationManager.desiredAccuracy = kCLLocationAccuracyBest; //设置隔多少米后更新位置信息 _locationManager.distanceFilter = 100; _locationManager.delegate = self; //5.开启定位 [_locationManager startUpdatingLocation]; } #pragma mark --CLLocationManagerDelegate--- //权限状态改变 -(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status { NSLog(@"%i",status); //给出相应提示信息 if (status != kCLAuthorizationStatusAuthorizedAlways) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请设置程序允许访问位置信息" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alert show]; } else { [_locationManager startUpdatingLocation]; } } //更新到用户位置信息 -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { CLLocation *location = [locations firstObject]; NSLog(@"%f-%f",location.coordinate.latitude,location.coordinate.longitude); //反编码 CLGeocoder *geo = [[CLGeocoder alloc] init]; [geo reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) { NSLog(@"%@",error); }]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
相关文章推荐
- web开发人员须知的web缓存知识–将数据缓存到浏览器端Net实现
- 使用JDBC插入数据到ORACLE,使用标识列自增列。
- C++处理字符串
- Python 冒泡排序
- VMware Fusion使用共享模式下虚拟机断网的解决方案
- Windows10 删除已经保存的WIFI热点
- 第二周项目-c++语言中函数参数传递的三种方式
- Oracle中用触发器实现自动记录表数据被修改的历史信息
- Win7 64位 Anaconda Lasagne 安装教程,防止错误,另Lasagne人脸facial points detect
- HDU 5439 Aggregated Counting 找规律+二分
- Oracle如何建立自增主键
- 编写Addition类,该类中应包含一组实现两数相加运算的重载方法
- Head First Python 第二章 函数模块&第三章 文件与异常&第四章 持久存储&第五章 处理数据
- HTML的基本
- LCA的暴力查询
- testlink的使用步骤
- 【Linux】RedHat9.0 利用sftp传文件
- QQ简单登录
- 程序包的管理:rpm及yum
- Linux php出现中文乱码的解决方法