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存儲了持續(xù)更新的位置坐標(biāo)值,取最后一個值為最新位置,如果不想讓其持續(xù)更新位置,則在此方法中獲取到一個值之后讓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) {
// 提示用戶出錯原因,可按住Option鍵點(diǎn)擊 KCLErrorDenied的查看更多出錯信息,可打印error.code值查找原因所在
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(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中UIAlertController設(shè)置自定義標(biāo)題與內(nèi)容的方法
UIAlertController是iOS8推出的新概念,取代了之前的 UIAlertView和UIActionSheet(雖然現(xiàn)在仍可以使用,但是會有警告)。下面這篇文章主要給大家介紹了關(guān)于iOS中UIAlertController如何設(shè)置自定義標(biāo)題與內(nèi)容的相關(guān)資料,需要的朋友可以參考下。2017-10-10
iOS利用余弦函數(shù)實(shí)現(xiàn)卡片瀏覽工具
這篇文章主要為大家詳細(xì)介紹了iOS利用余弦函數(shù)實(shí)現(xiàn)卡片瀏覽工具,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-04-04
iOS中設(shè)置網(wǎng)絡(luò)超時時間+模擬的方法詳解
這篇文章主要介紹了在iOS中設(shè)置網(wǎng)絡(luò)超時時間+模擬的方法,文中介紹的非常詳細(xì),相信對大家具有一定的參考價值,需要的朋友們下面來跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。2017-04-04
iOS小數(shù)取整的方法(ceil?floor?round)示例
這篇文章主要為大家介紹了iOS小數(shù)取整的方法(ceil?floor?round)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
深入講解iOS開發(fā)中應(yīng)用數(shù)據(jù)的存儲方式
這篇文章主要介紹了iOS開發(fā)中應(yīng)用數(shù)據(jù)的存儲方式,包括plistXML屬性列表和NSKeydeArchiver歸檔兩個部分,需要的朋友可以參考下2015-12-12
IOS數(shù)字鍵盤左下角添加完成按鈕的實(shí)現(xiàn)方法
這篇文章主要介紹了IOS數(shù)字鍵盤左下角添加完成按鈕的實(shí)現(xiàn)方法的相關(guān)資料,希望通過本文能實(shí)現(xiàn)類似這樣的功能,需要的朋友可以參考下2017-08-08

