iOS自动定位
需要在info.plist配置使用隐私数据
<key>NSLocationAlwaysUsageDescription</key>
<string>App需要您的同意,才能始终访问位置</string>
<key>NSLocationUsageDescription</key>
<string>App需要您的同意,才能访问位置</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>App需要您的同意,才能在使用期间访问位置</string>
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>
@property (nonatomic,strong) CLLocationManager * locationManager;
@end
@implementation ViewController
#pragma mark - ###### 定位 快捷键 mark
#pragma mark - ###### 定位 快捷键 mark
- (void)locate {
if([CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
NSLog(@"没打开");
// UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"打开定位开关" message:@"请点击确认打开定位服务" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
// [alertView show];
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
}else{
//判断定位功能是否打开
if ([CLLocationManager locationServicesEnabled]) {
self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager requestAlwaysAuthorization];
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
}else{
[self.locationManager requestAlwaysAuthorization];
}
}
}
#pragma mark CoreLocation delegate
//定位失败则执行此代理方法
//定位失败弹出提示框,点击"打开定位"按钮,会打开系统的设置,提示打开定位服务
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
UIAlertController * alertVC = [UIAlertController alertControllerWithTitle:@"允许\"定位\"提示" message:@"请在设置中打开定位" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * ok = [UIAlertAction actionWithTitle:@"打开定位" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//打开定位设置
NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:settingsURL];
}];
UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alertVC addAction:cancel];
[alertVC addAction:ok];
}
//定位成功
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
[self.locationManager stopUpdatingLocation];
CLLocation *currentLocation = [locations lastObject];
// 广州经纬度
// CLLocationDegrees la = 23.128594;
// CLLocationDegrees lo = 113.238879;
// currentLocation = [[CLLocation alloc] initWithLatitude:la longitude:lo];
// 上海经纬度
// CLLocationDegrees la = 31.23;
// CLLocationDegrees lo = 121.47;
// currentLocation = [[CLLocation alloc] initWithLatitude:la longitude:lo];
// 茅台镇
// CLLocationDegrees la = 27.8657885049;
// CLLocationDegrees lo = 106.3723754883;
// currentLocation = [[CLLocation alloc] initWithLatitude:la longitude:lo];
CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
//反编码
__weak typeof(self) weakSelf = self;
[geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (placemarks.count > 0) {
CLPlacemark *placeMark = placemarks[0];
// 国家
NSString *country = placeMark.addressDictionary[@"Country"];
// 省
NSString *province = placeMark.addressDictionary[@"State"];
// 城市
NSString *city = placeMark.addressDictionary[@"City"];
// 区、县
NSString *zone = placeMark.addressDictionary[@"SubLocality"];
// 最小地址
NSString *detail = placeMark.addressDictionary[@"Name"];
// 行政区划的接到、镇、乡
NSString *street = nil;
// 完全地址
NSString *name = [((NSArray *)placeMark.addressDictionary[@"FormattedAddressLines"]) firstObject];
if ([name hasSuffix:detail]) {
street = detail;
detail = @"";
}else{
NSRange fromRange = [name rangeOfString:placeMark.addressDictionary[@"SubLocality"]];
NSRange toRange = [name rangeOfString:placeMark.addressDictionary[@"Street"]];
NSInteger from = fromRange.location+fromRange.length;
NSInteger to = toRange.location;
NSInteger length = to-from;
NSRange streetRange = NSMakeRange(from, length);
// 行政区划的接到、镇、乡
street = [name substringWithRange:streetRange];
}
if (!placeMark.locality) {
NSLog(@"无法定位当前城市");
}
NSLog(@"%@",placeMark.name);//具体地址: xx市xx区xx街道
}
else if (error == nil && placemarks.count == 0) {
NSLog(@"No location and error return");
}
else if (error) {
NSLog(@"location error: %@ ",error);
}
}];
}
@end
网友评论