[iPhone硬件]-GPS定位的使用

iPhone中GPS定位如何使用

关键词

1.info.plist配置授权描述
2.引入库
3.CLLocationManager的使用

info.plist配置

在info.plist中根据情况加入以下两个string类型配置项,并填写描述

1.NSLocationAlwaysUsageDescription
2.NSLocationWhenInUseUsageDescription

引入库

-引入CoreLocation.framework
-在原文件中引入 <CoreLocation/CoreLocation.h>

代码实现


// 初始化

2.- (void)setupLocationManager {

3.    _locationManager = [[CLLocationManager alloc] init];

4.    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;

5.    _locationManager.distanceFilter = kCLDistanceFilterNone;

6.
7.    if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {

8.        [_locationManager requestWhenInUseAuthorization];

9.    }

10.}

11.
12.// 调用此方法开始定位

13.- (void) startUpdatingLocation {

14.    if ([CLLocationManager locationServicesEnabled]) {

15.        _shouldHandleLocation = YES;

16.        _locationManager.delegate = self;

17.        [_locationManager startUpdatingLocation];

18.    } else {

19.        [SVProgressHUD showErrorWithStatus:@"请开启定位功能!"];

20.    }

21.}

22.
23.#pragma mark - CLLocationManagerDelegate

24.- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

25.{

26.    CLLocation *newLocation = [locations lastObject];

27.    if (_shouldHandleLocation) {

28.        _shouldHandleLocation = NO;

29.        _latitude = newLocation.coordinate.latitude;

30.        _longitude = newLocation.coordinate.longitude;

31.
32.        [self commitCheckinInfo];

33.    }

34.
35.    _locationManager.delegate = nil;

36.    [_locationManager stopUpdatingLocation];

37.}

38.
39.- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

40.{

41.    [SVProgressHUD showErrorWithStatus:@"定位失败!"];

42.    _locationManager.delegate = nil;

43.    [manager stopUpdatingLocation];

44.}

posted @ 2016-02-01 18:47  HelloVoidWorld  阅读(996)  评论(0编辑  收藏  举报