IOS定位

 1 #import "ViewController.h"
 2 #import <CoreLocation/CoreLocation.h>
 3 @interface ViewController ()<CLLocationManagerDelegate>{
 4     CLLocationManager *_manager;//位置管理器
 5 }
 6 
 7 @end
 8 
 9 @implementation ViewController
10 
11 - (void)viewDidLoad {
12     [super viewDidLoad];
13     
14     if ([CLLocationManager locationServicesEnabled]) {
15         
16         NSLog(@"允许定位");
17         
18         _manager = [[CLLocationManager alloc] init];//初始化
19         
20         //判断用户是否选择了位置访问权限
21         if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
22             //如果尚未选择,则重新弹出请求
23             [_manager requestWhenInUseAuthorization];
24         }
25         
26         //设置代理
27         _manager.delegate = self;
28      
29         _manager.distanceFilter = 10;//设置定位频率多少米访问一次位置
30         
31         _manager.desiredAccuracy = kCLLocationAccuracyBest;//定位的精确度
32 
/*

extern const CLLocationAccuracy kCLLocationAccuracyBest;//最好的位置精度

extern const CLLocationAccuracy kCLLocationAccuracyNearestTenMeters;最近的10米位置精度

extern const CLLocationAccuracy kCLLocationAccuracyHundredMeters;//几百米的位置精度

extern const CLLocationAccuracy kCLLocationAccuracyKilometer;//几公里

extern const CLLocationAccuracy kCLLocationAccuracyThreeKilometers;//3公里

*/        
33         [_manager startUpdatingLocation];//开始定位
34         
35     //    [self getInfoBuyAddress];
36         
37         [self getInfoBuyCoordinate];
38     }
39     
40 }
41 //定位失败
42 -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
43     NSLog(@"---%@",error);
44 }
45 
46 -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
47     NSLog(@"===%zi---%@",locations.count,locations);
48     
49  //   CLLocation *location = [locations firstObject];
50     
51 //    NSLog(@"纬度:%f,经度:%f",location.coordinate.latitude,location.coordinate.longitude);
52     
53     
54 }
55 
56 //地理编码:根据地址信息,获取其对应的经纬度等信息
57 -(void)getInfoBuyAddress{
58     
59     CLGeocoder *geo = [[CLGeocoder alloc] init];//初始化编码管理
60     
61     //地理编码,传入地址,得到具体信息
62     [geo geocodeAddressString:@"郑州科技学院" completionHandler:^(NSArray *placemarks, NSError *error) {
63        
64 //        NSLog(@"--错误信息:%@",error);
65 //        
66       //  NSLog(@"---数量:%zi   信息:%@",placemarks.count,placemarks);
67         
68         CLPlacemark *place = [placemarks firstObject];
69         
70         NSLog(@"经纬度:%f,%f",place.location.coordinate.longitude,place.location.coordinate.latitude);//经纬度
71         NSLog(@"街道:%@",place.ocean);
72         
73     }];
74 }
75 
76 //反向地理编码:根据经纬度得到位置信息
77 
78 -(void)getInfoBuyCoordinate{
79     CLGeocoder *geo = [[CLGeocoder alloc] init];
80     
81     CLLocation *location = [[CLLocation alloc] initWithLatitude:34.708517 longitude:113.510850];//通过经纬度定义位置对象
82     
83     //传入位置对象,得到对应的地址信息
84     [geo reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
85         NSLog(@"数量:%zi   反向:%@",placemarks.count,placemarks);
86         
87         CLPlacemark *place = [placemarks firstObject];
88         
89         NSLog(@"经纬度:%f,%f",place.location.coordinate.longitude,place.location.coordinate.latitude);//经纬度
90         NSLog(@"街道:%@",place.ocean);
91 
92     }];
93 }

 

posted @ 2015-12-21 16:42  刘冠  阅读(183)  评论(0编辑  收藏  举报