1. IOS定位服务的开启与基本设置
1. 要想使用IOS中的定位服务首先需要包含头文件CoreLocation/CoreLocation.h,在interface中声明一个属性locationManager
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <CLLocationManagerDelegate>
// 定位管理器
@property (strong, nonatomic) CLLocationManager * locationManager;
@end
2. 懒加载
@implementation ViewController
- (CLLocationManager *)locationManager {
if (!_locationManager) {
// 实例化管理器
_locationManager = [[CLLocationManager alloc] init];
// 设置管理器类型为普通
_locationManager.activityType = CLActivityTypeOther;
// 设置精度为最高
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
// 设置自动过滤值
_locationManager.distanceFilter = 10;
// 设置代理
// 协议中的方法和定位结果有关
_locationManager.delegate = self;
}
return _locationManager;
}
@end
2. 检测应用程序的授权状态
// 获取定位服务授权状态
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
// 判断是否已经授权
if (status == kCLAuthorizationStatusNotDetermined) {
NSLog(@"未授权状态");
// 当使用应用程序时使用定位服务
[self.locationManager requestWhenInUseAuthorization];
} else if (status == kCLAuthorizationStatusDenied) {
NSLog(@"授权被拒绝");
// 创建一个模态警告视图控制器
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"亲,你还没有开启定位服务,现在设置好吗?" preferredStyle:UIAlertControllerStyleActionSheet];
// 打开设置的按钮
UIAlertAction *openAction = [UIAlertAction actionWithTitle:@"打开设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// 点击设置按钮的回调方法
// 打开应用程序的设置
NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
// 通过UIApplication对象的openURL方法可以
// 打电话 - tel://号码
// 发短信 - sms://号码
// 打开网页 - http(s)://网址
// 打开App Store - itms-apps://应用地址
// NSURL *url2 = [NSURL URLWithString:@"tel://1008611"];
// [[UIApplication sharedApplication] openURL:url2];]
[[UIApplication sharedApplication] openURL:url];
}];
// 取消按钮
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
// 将两个UIAlertAction(相当于两颗按钮)加到视图控制器上
[alertController addAction:openAction];
[alertController addAction:cancelAction];
// 以模态的方式显示警告视图控制器
[self presentViewController:alertController animated:YES completion:nil];
}
3. 开始定位
// 开始定位
[self.locationManager startUpdatingLocation];
// 通过协议方法获取定位信息
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
// locations是获取的位置信息集合
NSLog(@"%@", locations.firstObject);
}
4. 范围监测
#pragma mark - 监测区域
- (void) monitorRange {
// 监测中心
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(30.000000, 104.000000);
// 创建监测范围对象
// 参数1:监测中心
// 参数2:监测半径
// 参数3:标识
CLCircularRegion * region = [[CLCircularRegion alloc] initWithCenter:center radius:200 identifier:@"home"];
// 开始检测目标范围
[self.locationManager startMonitoringForRegion:region];
}
#pragma mark - CLLocationManagerDelegate
// 进入监测范围
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
NSLog(@"你已进入监测区域");
}
// 离开监测范围
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
NSLog(@"你已离开了监测区域");
}
5. 地理编码和反编码
#import "ZFGeocoder.h"
#import <CoreLocation/CoreLocation.h>
@implementation ZFGeocoder
// 通过类方法创建一个地理编码管理器
+ (instancetype) createGeocoder {
ZFGeocoder * geocoder = nil;
if (!geocoder) {
geocoder = [[ZFGeocoder alloc] init];
}
return geocoder;
}
// 将地址编码编成对应的经纬度
+ (void)getCoordinateWithAddress:(NSString *)address didFinished:(void (^)(CLLocationCoordinate2D))coordinate {
//将地址编码成对应的经纬度
[[self createGeocoder] geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//返回值
CLLocationCoordinate2D tcoordinate = CLLocationCoordinate2DMake(0, 0);
for (CLPlacemark * mark in placemarks) {
//拿到经纬度
tcoordinate = mark.location.coordinate;
}
//调用block传值
coordinate(tcoordinate);
}];
}
// 将经纬度进行反编码
+ (void)getAddressWithCoordinate:(CLLocationCoordinate2D)coordinate didFinished:(void (^)(NSDictionary *))address {
//拿到解析器
CLGeocoder * geo = [self createGeocoder];
//反编码
CLLocation * location = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
[geo reverseGeocodeLocation:location completionHandler:^(NSArray * placemarks, NSError * error) {
NSDictionary * dict;
//参数1:反编码后的结果
for (CLPlacemark * mark in placemarks) {
//拿到返回值
dict = mark.addressDictionary;
// NSLog(@"%@", mark.addressDictionary);
// //获取详细信息
// NSLog(@"%@", mark.addressDictionary[@"FormattedAddressLines"][0]);
// //国家
// NSLog(@"国家:%@",mark.addressDictionary[@"Country"]);
//
// //街道:
// NSLog(@"街道:%@", mark.addressDictionary[@"Street"]);
}
//调用block传值
address(dict);
}];
}
@end