备注:本教程只考虑iOS 8.0+,测试切勿忘了权限
iOS开发中CoreLocation是一个与定位有关的类,本教程将利用CoreLocation来实现三个小功能,获取经纬度、区域位置监听、地理位置编码。
1.获取经纬度
//
// LocationController.m
// CoreLocation
//
// Created by LouKit on 2017/2/17.
// Copyright ? 2017年 LouKit. All rights reserved.
//#import "LocationController.h"
#import <CoreLocation/CoreLocation.h>@interface LocationController ()<CLLocationManagerDelegate>@property (nonatomic,strong) CLLocationManager *locMgr;
@property (nonatomic, strong) CLLocation *lastLoc;@end@implementation LocationController-(CLLocationManager *)locMgr{if(![CLLocationManager locationServicesEnabled]){NSLog(@"定位服务没有开启不可用");return nil;}if (!_locMgr) {_locMgr = [[CLLocationManager alloc]init];_locMgr.delegate = self;//ios9 以上才有//_locMgr.allowsBackgroundLocationUpdates = YES;//前台定位//[_locMgr requestWhenInUseAuthorization];//前后台定位[_locMgr requestAlwaysAuthorization];//一些属性设置//定位精度/**kCLLocationAccuracyBestForNavigationCLLocationAccuracy kCLLocationAccuracyBest;CLLocationAccuracy kCLLocationAccuracyNearestTenMeters;CLLocationAccuracy kCLLocationAccuracyHundredMeters;CLLocationAccuracy kCLLocationAccuracyKilometer;CLLocationAccuracy kCLLocationAccuracyThreeKilometers;**/_locMgr.desiredAccuracy = kCLLocationAccuracyBest;//定位频率_locMgr.distanceFilter = 100;//每隔100米定位一次}return _locMgr;
}- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor yellowColor];// 开始定位用户的位置if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied){//doing}else{//一次定位//[self.locMgr requestLocation];[self.locMgr startUpdatingLocation];}[self countLineDistance];}#pragma mark - CLLocationManagerDelegate//当前位置