地圖定位功能的使用

定位功能

1.先導(dǎo)入地圖的庫
// 定位服務(wù)
#import <CoreLocation/CoreLocation.h>

2.創(chuàng)建定位管理對(duì)象
@property (nonatomic, strong) CLLocationManager * locationManager; 

3.設(shè)置代理
@interface AppDetailViewController ()<CLLocationManagerDelegate>
  
4.協(xié)議內(nèi)容
// 授權(quán)狀態(tài)改變時(shí)的回調(diào),第一次進(jìn)入App或者第一次請(qǐng)求用戶權(quán)限
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    // 當(dāng)授權(quán)完成時(shí),啟動(dòng)定位服務(wù),獲取定位信息
    if (status >= kCLAuthorizationStatusAuthorizedAlways) {
        [manager startUpdatingLocation];
    }
    else if (status == kCLAuthorizationStatusDenied)
    {
        // 不允許定位服務(wù),創(chuàng)建警示窗,對(duì)用戶進(jìn)行提示
        UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"警告" message:@"您已關(guān)閉定位,App的部分功能可能無法使用,請(qǐng)?jiān)谠O(shè)置中開啟,么么噠" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil];
        [alertView show];
    }
}
5.在info.plist中添加NSLocationWhenInUseUsageDescription

在后輸入用戶提示語


效果圖
6. 配置定位請(qǐng)求
- (void)configLocationManger
{
    self.locationManager = [[CLLocationManager alloc] init];
    // 精確度
    self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    // 刷新頻率 每過100米刷新一次
    self.locationManager.distanceFilter = 100.0;
    // 設(shè)置委托
    self.locationManager.delegate = self;
    
    // 請(qǐng)求用戶權(quán)限
    // 判斷當(dāng)前系統(tǒng)版本 8.0之前版本不需要用戶授權(quán)位置定位
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        // 請(qǐng)求WhenInUse權(quán)限
        [self.locationManager requestWhenInUseAuthorization];
        // 在info.plist文件中添加NSLocationWhenInUseUsageDescription
    }
    // 啟動(dòng)定位服務(wù)
    [self.locationManager startUpdatingLocation];
}

7.地球坐標(biāo)轉(zhuǎn)火星坐標(biāo)
導(dǎo)入庫


轉(zhuǎn)坐標(biāo)封裝的方法
//協(xié)議方法: 當(dāng)定位服務(wù)返回定位信息時(shí)的回調(diào)
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    if (locations.count > 0) {
        // 停止定位服務(wù)
        [manager stopUpdatingLocation];
        
        CLLocation * location = [locations lastObject];
        // 將地球坐標(biāo)轉(zhuǎn)換為火星坐標(biāo),返回火星坐標(biāo)
        CLLocation * marsLocation = [location locationMarsFromEarth];
        //獲取經(jīng)度
        marsLocation.coordinate.longitude;
        //獲取緯度
        marsLocation.coordinate.latitude;
       
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容