iOS獲取到用戶當(dāng)前位置
通過CoreLocation定位,獲取到用戶當(dāng)前位置,跟地圖中的定位不同。
一、導(dǎo)入CoreLocation.framework
二、#import <CoreLocation/CoreLocation.h>
三、聲明代理 <CLLocationManagerDelegate>
四、代碼實(shí)現(xiàn)
1、聲明
CLLocationManager *locationManager;//定義Manager // 判斷定位操作是否被允許 if([CLLocationManager locationServicesEnabled]) { CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease]; self.locationManager.delegate = self; }else { //提示用戶無法進(jìn)行定位操作 } // 開始定位 [locationManager startUpdatingLocation];
2、更新位置后代理方法,iOS6.0一下的方法
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { //latitude和lontitude均為NSString型變量 //緯度 self.latitude = [NSString stringWithFormat:@"%.4f", newLocation.coordinate.latitude]; //經(jīng)度 self.longitude = [NSString stringWithFormat:@"%.4f", newLocation.coordinate.longitude]; }
3、iOS6.0以上蘋果的推薦方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { //此處locations存儲(chǔ)了持續(xù)更新的位置坐標(biāo)值,取最后一個(gè)值為最新位置,如果不想讓其持續(xù)更新位置,則在此方法中獲取到一個(gè)值之后讓locationManager stopUpdatingLocation CLLocation *currentLocation = [locations lastObject]; CLLocationCoordinate2D coor = currentLocation.coordinate; self.latitude = coor.latitude; self.longitude = coor.longitude; //[self.locationManager stopUpdatingLocation]; }
4、更新失敗的方法
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { if (error.code == kCLErrorDenied) { // 提示用戶出錯(cuò)原因,可按住Option鍵點(diǎn)擊 KCLErrorDenied的查看更多出錯(cuò)信息,可打印error.code值查找原因所在 } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 帶有定位當(dāng)前位置的百度地圖前端web api實(shí)例代碼
- iOS中定位當(dāng)前位置坐標(biāo)及轉(zhuǎn)換為火星坐標(biāo)的方法
- Android獲取當(dāng)前位置的經(jīng)緯度數(shù)據(jù)
- 在AngularJS中如何使用谷歌地圖把當(dāng)前位置顯示出來
- JS利用cookie記憶當(dāng)前位置的防刷新導(dǎo)航效果
- Android實(shí)現(xiàn)Service獲取當(dāng)前位置(GPS+基站)的方法
- thinkphp獲取欄目和文章當(dāng)前位置的方法
- thinkphp實(shí)現(xiàn)面包屑導(dǎo)航(當(dāng)前位置)例子分享
- 鼠標(biāo)移到導(dǎo)航當(dāng)前位置的LI變色處于選中狀態(tài)
- 講解iOS開發(fā)中基本的定位功能實(shí)現(xiàn)
相關(guān)文章
iOS面試中如何優(yōu)雅回答B(yǎng)lock導(dǎo)致循環(huán)引用的問題
這篇文章主要給大家介紹了iOS面試中關(guān)于如何優(yōu)雅回答B(yǎng)lock導(dǎo)致循環(huán)引用的問題的相關(guān)資料,文中通過圖文介紹的非常相信,相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-03-03你應(yīng)該知道的tableViewCell行高計(jì)算處理
這篇文章主要給大家介紹了關(guān)于tableViewCell行高計(jì)算的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12iOS實(shí)現(xiàn)循環(huán)滾動(dòng)公告欄
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)循環(huán)滾動(dòng)公告欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03iOS動(dòng)畫解析之支付寶支付打鉤動(dòng)畫的實(shí)現(xiàn)方法
支付寶支付打鉤動(dòng)畫相信對(duì)大家來說都不陌生,這篇文章主要給大家介紹了關(guān)于iOS動(dòng)畫解析之支付寶支付動(dòng)畫的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位iOS開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11