码迷,mamicode.com
首页 > 移动开发 > 详细

IOS-CLLocationManager的定位功能

时间:2015-01-19 00:05:21      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:

注:在测定位功能的时候,比较多人会奇怪,为什么代码已经写好了,可是测试的时候,只有首次启动模拟器定位代码才有效。那是因为模拟器除了首次启动的时候会有默认的定位位置(默认位置是苹果美国总部),其它时候都需要你手动的去开启,在调试->位置->自定位置(填写经纬度)。

.h

1 #import <CoreLocation/CoreLocation.h>
2 
3 // 定位管理器,作用是:定位当前用户的经度和纬度
4 @property (nonatomic, strong) CLLocationManager *locationManager;
5 @property (nonatomic, strong) CLGeocoder *geocoder;

.m

 1 - (id)init
 2 {
 3     if (self = [super init]) {
 4         // 定位管理器
 5         _locationManager = [[CLLocationManager alloc] init];
 6         _geocoder = [[CLGeocoder alloc] init];
 7         // 当它定位完成,获得用户的经度和纬度时,会通知代理
 8         _locationManager.delegate = self;
 9     }
10     
11     return self;
12 }
13 
14 #pragma mark 启动定位
15 - (BOOL)startGetLocation
16 {
17     if ([CLLocationManager locationServicesEnabled]) {19         // 定位管理器 开始更新位置
20         [_locationManager startUpdatingLocation];
21         return YES;
22     } else {
23         DLog(@"定位服务当前可能尚未打开!");25         return NO;
26     }
27 }

CLGeocoder是一个地理编码器,可以把城市名反编码成经纬度或相反,这个后面会有用到。而以上就是用CLLocationManager启动定位的一个过程,先判断程序的定位服务是否允许,然后用startUpdatingLocation启动定位功能。

 1 #pragma mark - 定位管理器 代理方法
 2 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
 3 {
 4     // 1.既然已经定位到了用户当前的经纬度了,那么可以让定位管理器 停止定位了
 5     [_locationManager stopUpdatingLocation];
 6     // 2.然后,取出第一个位置,根据其经纬度,通过CLGeocoder反向解析,获得该位置所在的城市名称
 7     CLLocation *loc = [locations firstObject];
 8     
 9     [self getAddressByLocation:loc];
10 }
11 
12 #pragma mark 根据坐标取得地名
13 -(void)getAddressByLocation:(CLLocation *)location
14 {
15 //    __weak __typeof(self)weakSelf = self;
16     //反地理编码
17     [_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
18         CLPlacemark *placemark = [placemarks firstObject];
19         NSString *locality = placemark.locality;
20         NSRange range = [locality rangeOfString:@""];
21         if (range.length > 0) {
22             // 将最后一个字符【市】去掉
23             locality = [placemark.locality substringToIndex:placemark.locality.length - 1]; // 城市
24         }
25         NSLog(@"locality : %@", locality);
26         
27 //        if ([weakSelf.delegate respondsToSelector:@selector(didFinishLocations:error:)]) {
28 //            weakSelf.locationState = SuSLocationFinish;
29 //            [weakSelf.delegate didFinishLocations:[placemarks firstObject] error:error];
30 //        }
31     }];
32 }

启动后通过代理方法locationManager:didUpdateLocations:我们就可以获得定位位置的经纬度,然后就是需要用到CLGeocoder地理编码器的时候了,用方法reverseGeocodeLocation:completionHandler:反编码获得一个CLPlacemark对象,然后定位的信息就都存在该对象内了。CLPlacemark相应的属性可以参考以下代码:

 1      CLLocation *location = placemark.location;//位置
 2         CLRegion *region=placemark.region;//区域
 3         NSDictionary *addressDic= placemark.addressDictionary;//详细地址信息字典,包含以下部分信息
 4         NSString *name=placemark.name;//地名
 5         NSString *thoroughfare=placemark.thoroughfare;//街道
 6         NSString *subThoroughfare=placemark.subThoroughfare; //街道相关信息,例如门牌等
 7         NSString *locality=placemark.locality; // 城市
 8         NSString *subLocality=placemark.subLocality; // 城市相关信息,例如标志性建筑
 9         NSString *administrativeArea=placemark.administrativeArea; //
10         NSString *subAdministrativeArea=placemark.subAdministrativeArea; //其他行政区域信息
11         NSString *postalCode=placemark.postalCode; //邮编
12         NSString *ISOcountryCode=placemark.ISOcountryCode; //国家编码
13         NSString *country=placemark.country; //国家
14         NSString *inlandWater=placemark.inlandWater; //水源、湖泊
15         NSString *ocean=placemark.ocean; // 海洋
16         NSArray *areasOfInterest=placemark.areasOfInterest; //关联的或利益相关的地标

如若你还需要通过城市名来获取经纬度,则你可以使用以下方法:

 1 #pragma mark 根据地名确定地理坐标
 2 - (void)getCoordinateByAddress:(NSString *)address
 3 {
 4     //地理编码
 5     [_geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
 6         //取得第一个地标,地标中存储了详细的地址信息,注意:一个地名可能搜索出多个地址
 7         CLPlacemark *placemark = [placemarks firstObject];
 8         
 9         DLog(@"位置=%@-->纬度=%f----经度=%f", address, placemark.location.coordinate.latitude, placemark.location.coordinate.longitude);
10     }];
11 }

 

IOS-CLLocationManager的定位功能

标签:

原文地址:http://www.cnblogs.com/zw-h/p/4232608.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!