定位功能
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;
}
}