IOS學習之——定位服務2 實現基本定位
1 定位,主要是幹啥呢?
1 擷取位置相關資料
2 擷取位置相關資訊
由此,我想到了當年玩反恐精英裡面用瞄準鏡的瞄準的感覺:
2 那……怎樣定位呢?
1首先你得瞄準吧
locationManager=[[CLLocationManager alloc]init]; // 定義locationManage對象 開啟瞄準鏡 locationManager.delegate=self; // 實現代理 自己打槍 [locationManager requestAlwaysAuthorization ]; // 請求永久授權 你可以持槍多久? locationManager.desiredAccuracy=kCLLocationAccuracyBest; //設定定位精度 調節面准鏡遠近 //authorization 授權,認可 locationManager.distanceFilter=1000.0f; //設定獲得移動資訊最小距離 目標走多遠我需要調整?
這裡解釋一下:請求擷取授權有兩種情況: 1 requestAlwaysAuthorization 擷取永久授權 2 requestWhenInUseAuthorization 當使用的時候授權
設定精度有六種選擇:
1 kCLLocationAccuracyBestForNavigation 導航的最高精度,一般車載導航用(需外接電源)
2 kCLLocationAccuracyBest; 電池使用時候最高精度
3 kCLLocationAccuracyNearestTenMeters; 精確到10米
4 kCLLocationAccuracyHundredMeters; 精確到100米
5 kCLLocationAccuracyKilometer;精確到1000米
6 kCLLocationAccuracyThreeKilometers; 精確到3千米
2 你要把子彈上膛吧,打完了子彈得卸下來吧?
-(void)viewDidAppear:(BOOL)animated{ [locationManager startUpdatingLocation];// 開始更新位置資訊 子彈上膛}-(void)viewDidDisappear:(BOOL)animated{ [locationManager stopUpdatingLocation]; // 結束更新位置資訊 卸下子彈}
3 接下來可以打槍了吧(實現委託方法)
/*---------------------------locationmanagerdelegate的方法----------------------------------*/// 小譯:-()位置管理員:某位置管理員 已經更新的位置資訊:某位置資訊-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ CLLocation *currentLocation=[locations lastObject]; longtitudeTextField.text=[NSString stringWithFormat:@%3.5f,currentLocation.coordinate.longitude]; latitudeTextField.text=[NSString stringWithFormat:@%3.5f,currentLocation.coordinate.latitude]; highTextField.text=[NSString stringWithFormat:@%3.5f,currentLocation.altitude];}
4 這萬一,沒打中呢?(協議方法)
// 調用失敗// 小譯:-()位置管理員:某管理員 已經失敗的錯誤資訊:某錯誤資訊-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{ // 小譯:deny拒絕 if(error.code==kCLErrorDenied) { NSLog(@拒絕訪問); }else if (error.code==kCLErrorLocationUnknown) { NSLog(@位置資訊未知); }}
3 但是,這定位你想定就可以嗎?(小心人家不讓)
1要在info.list裡面添加兩個鍵哦
NSLocationAlwaysUsageDescription
NSLocationWhenInUseUsageDescription
2記得右鍵選擇下面的選項
3 效果呢?(設定裡面出現選項)
4 運行結果