地图与定位之定位
今天開(kāi)始總結(jié)一下地圖與定位。首先要使用定位就要引用框架CoreLocation.framework,在類文件中加入#import <CoreLocation/CoreLocation.h>,其次在IOS8中調(diào)用的時(shí)候要在Info.plist中加兩個(gè)Key,NSLocationAlwaysUsageDescription和NSLocationWhenInUseUsageDescription,具體參考http://www.cnblogs.com/kenshincui/p/4125570.html
#import "ViewController.h" @interface ViewController ()<CLLocationManagerDelegate> @property(nonatomic,strong)CLLocationManager *locationManager; @end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];_locationManager=[[CLLocationManager alloc]init];//判斷定位服務(wù)是否可用BOOL enable= [CLLocationManager locationServicesEnabled];// //CLAuthorizationStatus // typedef NS_ENUM(int, CLAuthorizationStatus) { // //用戶尚未做出決定是否啟用定位服務(wù) // kCLAuthorizationStatusNotDetermined = 0, // //沒(méi)有獲得用戶授權(quán)使用定位服務(wù),可能用戶沒(méi)有自己禁止訪問(wèn)授權(quán) // kCLAuthorizationStatusRestricted, // //用戶已經(jīng)明確禁止應(yīng)用使用定位服務(wù)或者當(dāng)前系統(tǒng)定位服務(wù)處于關(guān)閉狀態(tài) // kCLAuthorizationStatusDenied, // //應(yīng)用獲得授權(quán)可以一直使用定位服務(wù),即使應(yīng)用不在使用狀態(tài) // kCLAuthorizationStatusAuthorizedAlways NS_ENUM_AVAILABLE(NA, 8_0), // //使用此應(yīng)用過(guò)程中允許訪問(wèn)定位服務(wù) // kCLAuthorizationStatusAuthorizedWhenInUse NS_ENUM_AVAILABLE(NA, 8_0), // //已經(jīng)廢棄 // kCLAuthorizationStatusAuthorized NS_ENUM_DEPRECATED(10_6, NA, 2_0, 8_0, "Use kCLAuthorizationStatusAuthorizedAlways") = kCLAuthorizationStatusAuthorizedAlways //};if ([CLLocationManager authorizationStatus]<3&&enable) {//請(qǐng)求開(kāi)啟服務(wù)[_locationManager requestWhenInUseAuthorization];// [_locationManager requestAlwaysAuthorization];}else{ // _locationManager=[[CLLocationManager alloc]init];_locationManager.delegate=self;// kCLLocationAccuracyBestForNavigation //導(dǎo)航級(jí)別 // kCLLocationAccuracyBest; //最精確定位 // kCLLocationAccuracyNearestTenMeters; //十米級(jí)定位 // kCLLocationAccuracyHundredMeters; //百米級(jí)定位 // kCLLocationAccuracyKilometer; //千米級(jí)定位 // kCLLocationAccuracyThreeKilometers; //3千米級(jí)定位//設(shè)置精確度_locationManager.desiredAccuracy=kCLLocationAccuracyBest;//設(shè)置定位頻率,每隔多少米定位一次CLLocationDistance distance=10.0;_locationManager.distanceFilter=distance;//開(kāi)始定位跟蹤,開(kāi)始后按照用戶設(shè)定的更新頻率更新//(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;方法反饋定位信息[_locationManager startUpdatingLocation];if ([CLLocationManager headingAvailable]) {[_locationManager startUpdatingHeading];}} } //參數(shù)newHeading是一個(gè)CLHeading對(duì)象。CLHeading通過(guò)一組屬性來(lái)提供航向讀數(shù):magneticHeading和trueHeading。這些值的單位為度,類型為CLLocationDirection,即雙精度浮點(diǎn)數(shù) - (void)locationManager:(CLLocationManager *)managerdidUpdateHeading:(CLHeading *)newHeading {if(newHeading.headingAccuracy >=0){//trueHeading和magneticHeading分別表示真實(shí)航向和磁性航向。如果位置服務(wù)被關(guān)閉了,GPS和wifi就只能獲取magneticHeading(磁場(chǎng)航向)。只有打開(kāi)位置服務(wù),才能獲取trueHeading(真實(shí)航向)。NSString *headingDesc = [NSString stringWithFormat:@"%.0f degrees (true), %.0f degrees (magnetic)",newHeading.trueHeading,newHeading.magneticHeading];NSLog(@"%@",headingDesc);//關(guān)閉導(dǎo)航[_locationManager stopUpdatingHeading];}} //指定位置管理器是否向用戶顯示校準(zhǔn)提示。該提示將自動(dòng)旋轉(zhuǎn)設(shè)備360°。由于指南針總是自我校準(zhǔn),因此這種提示僅在指南針讀數(shù)劇烈波動(dòng)時(shí)才有幫助。當(dāng)設(shè)置為YES后,提示可能會(huì)分散用戶的注意力,或影響用戶的當(dāng)前操作 - (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager {return YES; }-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {//獲取位置CLLocation *location=[locations lastObject];//獲取二維坐標(biāo)經(jīng)緯度 結(jié)構(gòu)體類型 不用* // typedef struct { // CLLocationDegrees latitude; // CLLocationDegrees longitude; // } CLLocationCoordinate2D;CLLocationCoordinate2D coordinate2D=location.coordinate;NSLog(@"\n經(jīng)度:%lf\n緯度:%lf\n海拔:%lf\n航向:%lf\n速度:%lf\n位置精度(半徑負(fù)值無(wú)效):%lf\n海拔精度(負(fù)值無(wú)效)%lf",coordinate2D.longitude,coordinate2D.latitude,location.altitude,location.course,location.speed,location.horizontalAccuracy,location.verticalAccuracy);//停止定位[_locationManager stopUpdatingLocation];} //定位失敗 -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {NSLog(@"%ld",error.code); // error.code有以下枚舉 // typedef NS_ENUM(NSInteger, CLError) { // kCLErrorLocationUnknown = 0, // location is currently unknown, but CL will keep trying // kCLErrorDenied, // Access to location or ranging has been denied by the user // kCLErrorNetwork, // general, network-related error // kCLErrorHeadingFailure, // heading could not be determined // kCLErrorRegionMonitoringDenied, // Location region monitoring has been denied by the user // kCLErrorRegionMonitoringFailure, // A registered region cannot be monitored // kCLErrorRegionMonitoringSetupDelayed, // CL could not immediately initialize region monitoring // kCLErrorRegionMonitoringResponseDelayed, // While events for this fence will be delivered, delivery will not occur immediately // kCLErrorGeocodeFoundNoResult, // A geocode request yielded no result // kCLErrorGeocodeFoundPartialResult, // A geocode request yielded a partial result // kCLErrorGeocodeCanceled, // A geocode request was cancelled // kCLErrorDeferredFailed, // Deferred mode failed // kCLErrorDeferredNotUpdatingLocation, // Deferred mode failed because location updates disabled or paused // kCLErrorDeferredAccuracyTooLow, // Deferred mode not supported for the requested accuracy // kCLErrorDeferredDistanceFiltered, // Deferred mode does not support distance filters // kCLErrorDeferredCanceled, // Deferred mode request canceled a previous request // kCLErrorRangingUnavailable, // Ranging cannot be performed // kCLErrorRangingFailure, // General ranging failure // }; } - (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated. }@end?
?還有就是在模擬器中調(diào)試的問(wèn)題:可以選中模擬器的Debug——>Location 來(lái)定義經(jīng)緯度
轉(zhuǎn)載于:https://www.cnblogs.com/5ishare/p/4432346.html
總結(jié)
- 上一篇: 读《构建之法》1-5章
- 下一篇: Web性能优化分析