iOS自動(dòng)定位
需要在info.plist配置使用隱私數(shù)據(jù)
<key>NSLocationAlwaysUsageDescription</key>
<string>App需要您的同意,才能始終訪問(wèn)位置</string>
<key>NSLocationUsageDescription</key>
<string>App需要您的同意,才能訪問(wèn)位置</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>App需要您的同意,才能在使用期間訪問(wèn)位置</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(@"沒(méi)打開(kāi)");
// UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"打開(kāi)定位開(kāi)關(guān)" message:@"請(qǐng)點(diǎn)擊確認(rèn)打開(kāi)定位服務(wù)" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
// [alertView show];
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
}else{
//判斷定位功能是否打開(kāi)
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
//定位失敗則執(zhí)行此代理方法
//定位失敗彈出提示框,點(diǎn)擊"打開(kāi)定位"按鈕,會(huì)打開(kāi)系統(tǒng)的設(shè)置,提示打開(kāi)定位服務(wù)
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
UIAlertController * alertVC = [UIAlertController alertControllerWithTitle:@"允許\"定位\"提示" message:@"請(qǐng)?jiān)谠O(shè)置中打開(kāi)定位" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * ok = [UIAlertAction actionWithTitle:@"打開(kāi)定位" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//打開(kāi)定位設(shè)置
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];
// 廣州經(jīng)緯度
// CLLocationDegrees la = 23.128594;
// CLLocationDegrees lo = 113.238879;
// currentLocation = [[CLLocation alloc] initWithLatitude:la longitude:lo];
// 上海經(jīng)緯度
// CLLocationDegrees la = 31.23;
// CLLocationDegrees lo = 121.47;
// currentLocation = [[CLLocation alloc] initWithLatitude:la longitude:lo];
// 茅臺(tái)鎮(zhèn)
// 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];
// 國(guó)家
NSString *country = placeMark.addressDictionary[@"Country"];
// 省
NSString *province = placeMark.addressDictionary[@"State"];
// 城市
NSString *city = placeMark.addressDictionary[@"City"];
// 區(qū)盖溺、縣
NSString *zone = placeMark.addressDictionary[@"SubLocality"];
// 最小地址
NSString *detail = placeMark.addressDictionary[@"Name"];
// 行政區(qū)劃的接到频祝、鎮(zhèn)椒楣、鄉(xiāng)
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);
// 行政區(qū)劃的接到史飞、鎮(zhèn)糙俗、鄉(xiāng)
street = [name substringWithRange:streetRange];
}
if (!placeMark.locality) {
NSLog(@"無(wú)法定位當(dāng)前城市");
}
NSLog(@"%@",placeMark.name);//具體地址: xx市xx區(qū)xx街道
}
else if (error == nil && placemarks.count == 0) {
NSLog(@"No location and error return");
}
else if (error) {
NSLog(@"location error: %@ ",error);
}
}];
}
@end