IOS學習之——定位服務2 實現基本定位

來源:互聯網
上載者:User

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 運行結果
 
 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.