GPS定位
来源:互联网 发布:java九九乘法表代码 编辑:程序博客网 时间:2024/04/30 22:06
#import <CoreLocation/CoreLocation.h>//头文件
#define PATH @"http://api.map.baidu.com/geocoder?output=json&location=%f,%f&key=dc40f705157725fc98f1fee6a15b6e60"
@interface RootViewController ()<CLLocationManagerDelegate>
{
//lbs 定位管理
CLLocationManager *_manager;
}
@property (nonatomic,strong)CLLocationManager *manager;
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"开始定位" style:UIBarButtonItemStylePlain target:self action:@selector(begineLocation)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"停止定位" style:UIBarButtonItemStylePlain target:self action:@selector(endLocation)];
//创建管理器
[self initLocationManager];
}
/*
kCLLocationAccuracyBestForNavigation //导航时候用的精度
kCLLocationAccuracyBest;//比较高的精度
kCLLocationAccuracyNearestTenMeters; 10m内
kCLLocationAccuracyHundredMeters;100m
kCLLocationAccuracyKilometer;1000m
kCLLocationAccuracyThreeKilometers; 3000m
*/
- (void)initLocationManager {
if (!self.manager) {
//实例化一个管理对象
self.manager = [[CLLocationManager alloc] init];
//设置精度 精度越高越耗电
self.manager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
//精度大小 1m
self.manager.distanceFilter = 1;
/*
iOS之后 需要设置配置文件
1.在info.plist中添加 Privacy - Location Usage Description , NSLocationAlwaysUsageDescription
2.在代码中 [_manager requestAlwaysAuthorization];
//ios8特有,申请用户授权使用地理位置信息
*/
CGFloat version = [[[UIDevice currentDevice] systemVersion] floatValue];
//判断版本
if (version >= 8.0) {
//申请用户授权使用地理位置信息
[self.manager requestAlwaysAuthorization];
}
//如果要 获取定位的位置 那么需要设置代理
self.manager.delegate = self;
}
}
- (void)begineLocation {
//是否支持定位服务
if ([CLLocationManager locationServicesEnabled]) {
//开始定位
[self.manager startUpdatingLocation];
}else{
NSLog(@"没有gps");
}
}
//停止定位
- (void)endLocation {
[self.manager stopUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate协议
//当定位开始时 位置发生改变的时候 会一直调用
//会把定位的位置 放入 locations数组中
//这个数组实际上只有一个元素
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
NSLog(@"定位位置");
if (locations.count) {
//数组中只有一个元素
CLLocation *location = [locations lastObject];
//CLLocationCoordinate2D是一个结构体 内部存放的是经纬度
CLLocationCoordinate2D coordinate = location.coordinate;
NSLog(@"longitude:%f",coordinate.longitude);
NSLog(@"latitude:%f",coordinate.latitude);
#if 1
//官方自带的地理反编码
CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
//地理逆向编码 地理反编码 (把经纬度转化为真正的地址位置)
[geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
//回调这个block
//解析好之后会放在 placemarks 数组中(实际上就一个元素)
for (CLPlacemark *mark in placemarks) {
NSLog(@"name->%@",mark.name);
NSLog(@"country:%@",mark.country);
for (NSString *key in mark.addressDictionary) {
NSLog(@"%@",mark.addressDictionary[key]);
}
}
}];
#else
//异步下载数据 用百度的地理反编码
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *url = [NSString stringWithFormat:PATH,34.77274892, 113.67591140];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
//下载完成
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSDictionary *result = dict[@"result"];
NSLog(@"addr:%@",result[@"formatted_address"]);
});
#endif
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"定位失败");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#define PATH @"http://api.map.baidu.com/geocoder?output=json&location=%f,%f&key=dc40f705157725fc98f1fee6a15b6e60"
@interface RootViewController ()<CLLocationManagerDelegate>
{
//lbs 定位管理
CLLocationManager *_manager;
}
@property (nonatomic,strong)CLLocationManager *manager;
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"开始定位" style:UIBarButtonItemStylePlain target:self action:@selector(begineLocation)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"停止定位" style:UIBarButtonItemStylePlain target:self action:@selector(endLocation)];
//创建管理器
[self initLocationManager];
}
/*
kCLLocationAccuracyBestForNavigation //导航时候用的精度
kCLLocationAccuracyBest;//比较高的精度
kCLLocationAccuracyNearestTenMeters; 10m内
kCLLocationAccuracyHundredMeters;100m
kCLLocationAccuracyKilometer;1000m
kCLLocationAccuracyThreeKilometers; 3000m
*/
- (void)initLocationManager {
if (!self.manager) {
//实例化一个管理对象
self.manager = [[CLLocationManager alloc] init];
//设置精度 精度越高越耗电
self.manager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
//精度大小 1m
self.manager.distanceFilter = 1;
/*
iOS之后 需要设置配置文件
1.在info.plist中添加 Privacy - Location Usage Description , NSLocationAlwaysUsageDescription
2.在代码中 [_manager requestAlwaysAuthorization];
//ios8特有,申请用户授权使用地理位置信息
*/
CGFloat version = [[[UIDevice currentDevice] systemVersion] floatValue];
//判断版本
if (version >= 8.0) {
//申请用户授权使用地理位置信息
[self.manager requestAlwaysAuthorization];
}
//如果要 获取定位的位置 那么需要设置代理
self.manager.delegate = self;
}
}
- (void)begineLocation {
//是否支持定位服务
if ([CLLocationManager locationServicesEnabled]) {
//开始定位
[self.manager startUpdatingLocation];
}else{
NSLog(@"没有gps");
}
}
//停止定位
- (void)endLocation {
[self.manager stopUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate协议
//当定位开始时 位置发生改变的时候 会一直调用
//会把定位的位置 放入 locations数组中
//这个数组实际上只有一个元素
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
NSLog(@"定位位置");
if (locations.count) {
//数组中只有一个元素
CLLocation *location = [locations lastObject];
//CLLocationCoordinate2D是一个结构体 内部存放的是经纬度
CLLocationCoordinate2D coordinate = location.coordinate;
NSLog(@"longitude:%f",coordinate.longitude);
NSLog(@"latitude:%f",coordinate.latitude);
#if 1
//官方自带的地理反编码
CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
//地理逆向编码 地理反编码 (把经纬度转化为真正的地址位置)
[geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
//回调这个block
//解析好之后会放在 placemarks 数组中(实际上就一个元素)
for (CLPlacemark *mark in placemarks) {
NSLog(@"name->%@",mark.name);
NSLog(@"country:%@",mark.country);
for (NSString *key in mark.addressDictionary) {
NSLog(@"%@",mark.addressDictionary[key]);
}
}
}];
#else
//异步下载数据 用百度的地理反编码
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *url = [NSString stringWithFormat:PATH,34.77274892, 113.67591140];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
//下载完成
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSDictionary *result = dict[@"result"];
NSLog(@"addr:%@",result[@"formatted_address"]);
});
#endif
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"定位失败");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
0 0
- GPS定位
- GPS定位
- GPS定位
- GPS定位
- GPS定位
- gps定位
- GPS定位
- GPS定位
- GPS定位
- gps定位
- gps定位
- GPS定位
- GPS定位+经纬度定位
- GPS定位功能。GPS定位例子
- GPS定位原理
- GPS定位原理概述
- GPS车载定位系统
- GPS全球卫星定位导航系统
- 导航栏返回按钮设置为空
- Linux环境SVN命令行使用经验总结
- PHP 正则表示式
- JAVA托盘程序SystemTray的心得
- 一站式解决eclipse genymotion插件问题
- GPS定位
- 修改提示符PS1
- Android Hook框架Xposed原理与源代码分析
- Spring中配置和读取多个Properties文件-- PropertyPlaceholderConfigurer
- Objective-C Blocks Quiz
- 数据库图片导入
- 正则表达式
- Apache shutdown unexpectedly启动错误解决方法
- Android中Service(服务)详解