一幻捏、基本介紹
1盆犁、定位,基于GPS
定位是一個(gè)很常用的功能,打開地圖軟件后如果用戶允許軟件定位的話粘咖,軟件便會(huì)自動(dòng)鎖定到手機(jī)所在位置蚣抗,并且地圖上的位置會(huì)隨著手機(jī)的移動(dòng)而移動(dòng)
2、反編碼
將經(jīng)緯度轉(zhuǎn)化為地理坐標(biāo)
3瓮下、編碼
將地理坐標(biāo)轉(zhuǎn)化為經(jīng)緯度
二翰铡、定位
1、使用到的框架及相關(guān)的類
CoreLocation框架中的CLLocationManager類
CoreLocation框架中的CLLocation類
2讽坏、CLLocationManager锭魔,定位管理器,用來設(shè)置管理定位,設(shè)置定位的精度路呜、定位頻率等
常用的方法及屬性
1)當(dāng)前系統(tǒng)是否打開定位服務(wù)迷捧,在設(shè)置->隱私里控制,這里能夠控制手機(jī)上所有App的定位授權(quán)
+(BOOL)locationServiceEnabled
2)請(qǐng)求獲得應(yīng)用程序使用時(shí)定位服務(wù)授權(quán)胀葱,注意使用此方法前在要在info.plist中配置NSLocationWhenInUseUsageDescription
requestWhenInUseAuthorization
3)請(qǐng)求獲得應(yīng)用一直使用定位服務(wù)授權(quán)漠秋,注意使用此方法前要在info.plist中配置NSLocationAlwaysUsageDescription
requestAlwaysAuthorization
4)設(shè)置定位精度
_locationManager.desiredAccuracy = 10;
5)設(shè)置定位頻率
CLLocationDistance distance=1.0;
_locationManager.distanceFilter = distance;
PS:設(shè)置定位頻率定位頻率和定位精度并不應(yīng)當(dāng)越精確越好,需要視實(shí)際情況而定抵屿,因?yàn)樵骄_越耗性能庆锦,也就越費(fèi)電。
6)開始定位追蹤
startUpdatingLocation
7)停止定位追蹤
stopUpdatingLocation
8)開始導(dǎo)航方向追蹤
startUpdatingHeading
9)停止導(dǎo)航方向追蹤
stopUpdatingHeading
10)開始對(duì)某個(gè)區(qū)域進(jìn)行定位追蹤轧葛,開始對(duì)某個(gè)區(qū)域進(jìn)行定位后搂抒。如果用戶進(jìn)入或者走出某個(gè)區(qū)域會(huì)調(diào)用相應(yīng)的代理方法反饋相關(guān)信息
startMonitoringForRegion
11)停止對(duì)某區(qū)域追蹤
stopMonitoringForRegion
代理方法
1)位置發(fā)生改變后執(zhí)行
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;
2)導(dǎo)航方向發(fā)生變化后執(zhí)行
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading;
3)進(jìn)入某個(gè)區(qū)域之后執(zhí)行
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region;
4)走出某個(gè)區(qū)域之后執(zhí)行
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
5) 定位失敗
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error;
3艇搀、CLLocation:用于表示位置信息,包含地理坐標(biāo)求晶、海拔等信息焰雕,包含在CoreLoaction框架中
常用屬性
1) 經(jīng)緯度
coordinate
latitude:緯度
longitude:經(jīng)度
2) 海拔高度
altitude
3)位置的精度,為負(fù)表明值無效
horizontalAccuracy
4)海拔高度的精度芳杏,為負(fù)表明值無效
verticalAccuracy //垂直
horizontalAccuracy //豎直
5)速度
speed
6) 日期
timestamp
7)當(dāng)前樓層
floor.level
8)course當(dāng)前設(shè)備前進(jìn)的方向矩屁。該值為0°表示向北,90°表示向東蚜锨,180°表示向南档插,270°表示向西
course:0.0 ~ 359.9 真北方向?yàn)?.0
常用方法
1)得到兩個(gè)位置之間的距離,通過不斷累加亚再,來獲取總距離
[curLoc distanceFromLocation:nil];
2)得到兩次更新的時(shí)間之間的間隔郭膛,通過累加來獲取行駛總時(shí)間
[curLoc.timestamp timeIntervalSinceDate:lastLoc.timestamp];
五、實(shí)現(xiàn)定位步驟? ?
1氛悬、導(dǎo)入框架? ? #import<CoreLocation/CoreLocation.h>
2则剃、聲明全局的定位管理器
CLLocationManager *_locationManager;
3、實(shí)例化定位管理器
_locationManager = [[CLLocationManager alloc]init];
4如捅、判斷當(dāng)前系統(tǒng)是否打開定位服務(wù)
if ([CLLocationManager locationServicesEnabled] == NO) {
//判斷是否可以打開設(shè)置界面
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]) {
//跳轉(zhuǎn)到設(shè)置頁面
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
};
return;
}
5棍现、定位授權(quán),如果不授權(quán)就無法定位
[_locationManager requestWhenInUseAuthorization];
// 或[_locationManager requestAlwaysAuthorization];
6镜遣、在info.plist插入兩個(gè)字段己肮,不添加就無法定位
//都string類型,和第五步的兩種授權(quán)悲关,寫入的內(nèi)容會(huì)出現(xiàn)在定位授權(quán)提示框上
NSLocationWhenInUseUsageDescription//使用中授權(quán)描述
NSLocationAlwaysUsageDescription//永久授權(quán)描述
7谎僻、掛上代理
_locationManager.delegate = self;
8、設(shè)置定位精度
_locationManager.desiredAccuracy = 10;
9寓辱、設(shè)置定位頻率
_locationManager.distanceFilter = 10;
10艘绍、開始追蹤,如果不需要定位了記得停止定位,要不然會(huì)有很大的耗電量
11秫筏、實(shí)現(xiàn)代理方法? ? ? - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray*)locations{
}
//定位失敗
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
NSLog(@"定位失敗%@",error);
}
PS:總結(jié):如果定位失敗诱鞠,首先檢查一下幾點(diǎn):
1、定位管理器是否為全局變量
2这敬、info.plist中的字段是否配置
3航夺、在代碼中是否做授權(quán)處理
4、如果用模擬器的話崔涂,可能是因?yàn)闆]有給模擬器設(shè)置經(jīng)緯度敷存,點(diǎn)擊模擬器模擬器 -> 菜單欄Debug -> Location -> CustomLocation -> 設(shè)置經(jīng)緯度,如果想要讓模擬器自動(dòng)定位選擇Location —> Apple。
練習(xí):
1锚烦、測(cè)速
2、指南針
注意: 獲取用戶的設(shè)備朝向,不需要用戶進(jìn)行定位授權(quán)
0.使用之前先判斷方向服務(wù)是否可用
// magneticHeading : 距離磁北方向的角度
// headingAccuracy : 如果是負(fù)數(shù),代表當(dāng)前設(shè)備朝向不可用
if (newHeading.headingAccuracy < 0) {
return;
}
1.獲取手機(jī)設(shè)備朝向(使用的是”磁力計(jì)”傳感器)不需要用戶定位授權(quán)
2.設(shè)備朝向(使用的是”磁力計(jì)”傳感器)和航向course的區(qū)別(gps定位確定)
六帝雇、編碼與反編碼
1涮俄、使用到的框架及相關(guān)的類
CoreLocation框架中的CLGeocoder類
提供兩個(gè)方法實(shí)現(xiàn)編碼與反編碼
CoreLocation框架中的CLPlacemark類
地標(biāo)類,地標(biāo)中存儲(chǔ)了詳細(xì)的地址信息
2尸闸、CLGeocoder類
1)編碼方法
- (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;
2)反編碼方法
- (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;
3彻亲、CLPlacemark類
1) location:位置信息
2) region:范圍
3) addressDictionary:地址信息字典
更多信息
1、name:地名
2吮廉、thoroughfare:街道
3苞尝、subThoroughfare:街道的副標(biāo)題
4、locality:城市
5宦芦、subLocality:城市的相關(guān)信息
6宙址、administrativeArea:州 省
7、subAdministrativeArea:州省相關(guān)信息
8调卑、postalCode:郵政編碼
9抡砂、ISOcountryCode:國(guó)家編碼
10、country:國(guó)家
11恬涧、inlandWater:水源 湖泊
12注益、ocean:海洋
13、areasOfInterest:相關(guān)的地標(biāo)