iOS自動(dòng)定位

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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市拴测,隨后出現(xiàn)的幾起案子垫蛆,更是在濱河造成了極大的恐慌,老刑警劉巖嗤攻,帶你破解...
    沈念sama閱讀 218,858評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件毛嫉,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡妇菱,警方通過(guò)查閱死者的電腦和手機(jī)承粤,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)恶耽,“玉大人密任,你說(shuō)我怎么就攤上這事⊥导螅” “怎么了浪讳?”我有些...
    開(kāi)封第一講書人閱讀 165,282評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)涌萤。 經(jīng)常有香客問(wèn)我淹遵,道長(zhǎng)口猜,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書人閱讀 58,842評(píng)論 1 295
  • 正文 為了忘掉前任透揣,我火速辦了婚禮济炎,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘辐真。我一直安慰自己须尚,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,857評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布侍咱。 她就那樣靜靜地躺著耐床,像睡著了一般。 火紅的嫁衣襯著肌膚如雪楔脯。 梳的紋絲不亂的頭發(fā)上撩轰,一...
    開(kāi)封第一講書人閱讀 51,679評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音昧廷,去河邊找鬼堪嫂。 笑死,一個(gè)胖子當(dāng)著我的面吹牛木柬,可吹牛的內(nèi)容都是我干的皆串。 我是一名探鬼主播,決...
    沈念sama閱讀 40,406評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼弄诲,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼愚战!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起齐遵,我...
    開(kāi)封第一講書人閱讀 39,311評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤寂玲,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后梗摇,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體拓哟,經(jīng)...
    沈念sama閱讀 45,767評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評(píng)論 3 336
  • 正文 我和宋清朗相戀三年伶授,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了断序。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,090評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡糜烹,死狀恐怖违诗,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情疮蹦,我是刑警寧澤诸迟,帶...
    沈念sama閱讀 35,785評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響阵苇,放射性物質(zhì)發(fā)生泄漏壁公。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,420評(píng)論 3 331
  • 文/蒙蒙 一绅项、第九天 我趴在偏房一處隱蔽的房頂上張望紊册。 院中可真熱鬧,春花似錦快耿、人聲如沸囊陡。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,988評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)关斜。三九已至示括,卻和暖如春铺浇,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背垛膝。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 33,101評(píng)論 1 271
  • 我被黑心中介騙來(lái)泰國(guó)打工鳍侣, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人吼拥。 一個(gè)月前我還...
    沈念sama閱讀 48,298評(píng)論 3 372
  • 正文 我出身青樓倚聚,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親凿可。 傳聞我的和親對(duì)象是個(gè)殘疾皇子惑折,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,033評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容