ios GPS定位

#import <CoreLocation/CoreLocation.h>

.h文件中

@property (nonatomic,strong) CLLocationManager * manager;
@property (nonatomic,assign) BOOL isLocationed;

.m文件中

-(CLLocationManager *)manager{
    if (!_manager) {
        _manager = [[CLLocationManager alloc]init];
        _manager.delegate = self;
        _manager.desiredAccuracy = kCLLocationAccuracyBest;
    }
    return _manager;
}
- (void)findLocationCity{
    
    if ([CLLocationManager locationServicesEnabled]) {
        if ([self.manager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
            [self.manager requestWhenInUseAuthorization];
        }
        [self.manager startUpdatingLocation];
    }else{
        NSLog(@"提醒用戶:定位服務(wù)未開啟扩然,可在設(shè)置中進(jìn)行修改....");
    }
}
#pragma mark
#pragma mark - 獲取 GPS 經(jīng)度和緯度
- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray<CLLocation *> *)locations{
    
    if (!_isLocationed) {
        
        CLLocation * location1 = [locations lastObject];
        CLLocationCoordinate2D coordinate = location1.coordinate;
        CLGeocoder * geocoder = [[CLGeocoder alloc]init];
        CLLocation * location = [[CLLocation alloc]initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
        [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
            if (placemarks.count > 0) {
                CLPlacemark * placemark = [placemarks objectAtIndex:0];
                
                NSString * city = placemark.locality;
                
                if (!city) {
                    city = placemark.administrativeArea;
                }
                
                UIAlertController * ac = [UIAlertController alertControllerWithTitle:@"切換當(dāng)前城市:" message:city preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction * aa = [UIAlertAction actionWithTitle:@"確認(rèn)" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    
                    [WeatherView_Model weatherWithCurrentCity:city finishBlock:^(WeatherView_Model *weatherModel) {
                        self.weather_View.weatherModel = weatherModel;
                    } errorBlock:^{
                    }];
                }];
                [ac addAction:aa];
                
                UIAlertAction * aa2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
                [ac addAction:aa2];
                
                [self presentViewController:ac animated:YES completion:nil];
                
            }else if (error == nil && placemarks.count == 0){
                NSLog(@"No results were returned.");
            }else if (error != nil){
                NSLog(@"An error occurred = %@", error);
            }
        }];
        
        self.isLocationed = YES;
    }
    [self.manager stopUpdatingLocation];
}
#pragma mark
#pragma mark - 定位失敗返回
- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error{
    if (error.code == kCLErrorDenied) {
        NSLog(@"Error : %@",error);
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末艘儒,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子夫偶,更是在濱河造成了極大的恐慌界睁,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件兵拢,死亡現(xiàn)場離奇詭異翻斟,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)说铃,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進(jìn)店門访惜,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人腻扇,你說我怎么就攤上這事债热。” “怎么了幼苛?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵窒篱,是天一觀的道長。 經(jīng)常有香客問我,道長墙杯,這世上最難降的妖魔是什么济锄? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮霍转,結(jié)果婚禮上荐绝,老公的妹妹穿的比我還像新娘。我一直安慰自己避消,他們只是感情好低滩,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著岩喷,像睡著了一般恕沫。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上纱意,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天婶溯,我揣著相機(jī)與錄音,去河邊找鬼偷霉。 笑死迄委,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的类少。 我是一名探鬼主播叙身,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼硫狞!你這毒婦竟也來了信轿?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤残吩,失蹤者是張志新(化名)和其女友劉穎财忽,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體泣侮,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡即彪,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了旁瘫。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片祖凫。...
    茶點(diǎn)故事閱讀 40,090評論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖酬凳,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情遭庶,我是刑警寧澤宁仔,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站峦睡,受9級特大地震影響翎苫,放射性物質(zhì)發(fā)生泄漏权埠。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一煎谍、第九天 我趴在偏房一處隱蔽的房頂上張望攘蔽。 院中可真熱鬧,春花似錦呐粘、人聲如沸满俗。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽唆垃。三九已至,卻和暖如春痘儡,著一層夾襖步出監(jiān)牢的瞬間辕万,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工沉删, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留渐尿,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓矾瑰,卻偏偏與公主長得像涡戳,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子脯倚,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,033評論 2 355

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