iOS 8 开发运用系统自带CoreLocation 实现定位功能

运用系统自带的CoreLocation实现手机定位功能

.h

1.首先导入系统类库

#import <CoreLocation/CoreLocation.h>

2,创建CllocationManager 对象

{

CllocationManager * _locationManager;

}

.m

1.在.m中实现CllocationManager对象

self._locationManager = [[CllocationManager alloc]init];

2.// 设置代理

self._locationManager.delegate = self; 

3.设置精确度

self._locationManager.desiredAccuracy = KCLLocationAccuracyBest;

4.设置它的distanceFilter

self._locationManager.distanceFilter = 10.0f;

5.设置始终允许访问位置信息

[self._locationManager requestAlwaysAuthorization];

6.开始定位

[self._locationManager startUpdatingLocation];

7.实现协议方法

 1 -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
 2 {
 3 //进行地理编码
 4 CLGeocoder * geocoder = [[CLGeocoder alloc]init];
 5 //因为我这里是模拟器 所以使用的是[locations lastObject]  如果是真机得话可以试一下[locations lastObject]和[locations firstObject]的区别
 6 [geocoder reverseGeocodeLocation:[locations lastObject] completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error){
 7   for (CLPlacemark * placemark in placemarks) {
 8             NSDictionary * test = [placemark addressDictionary];
 9             [self._localtionManager stopUpdatingLocation];
10             NSLog(@"%@", [test objectForKey:@"State"]);
11 }];
12 };
13 -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
14 {
15     if ([error code] == kCLErrorDenied) {
16         NSLog(@"访问被拒");
17     }
18     if ([error code] == kCLErrorLocationUnknown) {
19         NSLog(@"无法获取位置信息");
20     }
21 }

注意: 在ios8上运用CllocationManager进行定位的话,需要对.plist 文件进行配置 需要在.plist文件里面添加如下;

1,

NSLocationAlwaysUsageDescription

2,

NSLocationWhenInUseUsageDescription

添加完成之后就可以进行定位操作了.....祝大家生活愉快!!!!

posted on 2015-10-29 14:37  Smile_闪电  阅读(261)  评论(0编辑  收藏  举报

导航