iOS 定位功能的实现
生活随笔
收集整理的這篇文章主要介紹了
iOS 定位功能的实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、導入框架
Xcode中添加“CoreLocation.framework”2、導入主頭文件
#import <CoreLocation/CoreLocation.h>3、聲明管理器和代理
@interface ViewController ()<CLLocationManagerDelegate>@property (nonatomic, strong) CLLocationManager* locationManager;@end4、在appDelegate或控制器中 初始化管理器
//定位管理器self.locationManager=[[CLLocationManager alloc]init];if (![CLLocationManager locationServicesEnabled]) {SSLog(@"定位服務當前可能尚未打開,請設置打開!");//return; }//如果沒有授權則請求用戶授權if ([CLLocationManager authorizationStatus]==kCLAuthorizationStatusNotDetermined){[self.locationManager requestWhenInUseAuthorization];// 當使用app時獲取位置 // [self.locationManager requestAlwaysAuthorization];// 一直獲取位置 }//設置代理self.locationManager.delegate = self;//設置定位精度self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;//定位頻率,每隔多少米定位一次CLLocationDistance distance = 10.0;//十米定位一次self.locationManager.distanceFilter=distance;//啟動跟蹤定位[self.locationManager startUpdatingLocation];5、代理方法
// 跟蹤定位代理方法,每次位置發生變化即會執行(只要定位到相應位置) -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{CLLocation *location=[locations firstObject];//取出第一個位置CLLocationCoordinate2D coordinate=location.coordinate;//位置坐標self.coordinate = coordinate;SSLog(@"經度:%f,緯度:%f,海拔:%f,航向:%f,行走速度:%f",coordinate.longitude,coordinate.latitude,location.altitude,location.course,location.speed);//如果不需要實時定位,使用完即使關閉定位服務 [self.locationManager stopUpdatingLocation]; }6、在plist文件中設置
NSLocationWhenInUseUsageDescription?? 后面對應的是使用時對用戶的說明
轉載于:https://www.cnblogs.com/shen5214444887/p/5941615.html
總結
以上是生活随笔為你收集整理的iOS 定位功能的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 字符串野指针 百练2681
- 下一篇: 第七单元总结