iOS CLLocationManager定位,IOS8注意

來源:互聯網
上載者:User

iOS CLLocationManager定位,IOS8注意

今天下午動手用了IOS內建的定位,結果在網上看了很多教程,也將範例程式碼直接運行,但就是一直無法擷取位置,代碼如下:

首先匯入CoreLocation.framework,然後再引入標頭檔#import

定義屬性

@property (nonatomic , strong)CLLocationManager *locationManager;
然後使用代理 CLLocationManagerDelegate

 

 

- (void)locate{    // 判斷定位操作是否被允許    if([CLLocationManager locationServicesEnabled]) {        //定位初始化        _locationManager=[[CLLocationManager alloc] init];        _locationManager.delegate=self;        _locationManager.desiredAccuracy=kCLLocationAccuracyBest;        _locationManager.distanceFilter=10;        [_locationManager startUpdatingLocation];//開啟定位    }else {        //提示使用者無法進行定位操作        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@提示 message:@定位不成功 ,請確認開啟定位 delegate:nil cancelButtonTitle:@取消 otherButtonTitles:@確定, nil];        [alertView show];    }    // 開始定位    [_locationManager startUpdatingLocation];}#pragma mark - CoreLocation Delegate-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{    //此處locations儲存了持續更新的位置座標值,取最後一個值為最新位置,如果不想讓其持續更新位置,則在此方法中擷取到一個值之後讓locationManager stopUpdatingLocation    CLLocation *currentLocation = [locations lastObject];    // 擷取當前所在的城市名    CLGeocoder *geocoder = [[CLGeocoder alloc] init];    //根據經緯度反向地理編譯出地址資訊    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *array, NSError *error)    {        if (array.count > 0)        {            CLPlacemark *placemark = [array objectAtIndex:0];            //NSLog(@%@,placemark.name);//具體位置            //擷取城市            NSString *city = placemark.locality;            if (!city) {                //四大直轄市的城市資訊無法通過locality獲得,只能通過擷取省份的方法來獲得(如果city為空白,則可知為直轄市)               city = placemark.administrativeArea;            }            cityName = city;            NSLog(@定位完成:%@,cityName);            //系統會一直更新資料,直到選擇停止更新,因為我們只需要獲得一次經緯度即可,所以擷取之後就停止更新            [manager stopUpdatingLocation];         }else if (error == nil && [array count] == 0)         {            NSLog(@No results were returned.);         }else if (error != nil)         {             NSLog(@An error occurred = %@, error);         }    }];}

viewDidLoad中調用了locate之後一直沒有資料返回,代理也不執行,在網上找了很多教程,代碼大多大同小異,但是結果都是不理想,偶然之間看到ios8的定位和低版本的不同,My Phone剛好是ios8,便點開那個部落格去看看, 部落格請點此進入

 

以下為該部落格摘要:

在iOS8以前的版本中,我們使用CLLocationManager定位是沒有問題的,最近在iOS8系統中卻無法定位了。。。。這是一大問題啊!

iOS8中使用CoreLocation定位

1、在使用CoreLocation前需要調用如下函數【iOS8專用】:
iOS8對定位進行了一些修改,其中包括定位授權的方法,CLLocationManager增加了下面的兩個方法:
(1)始終允許訪問位置資訊

- (void)requestAlwaysAuthorization;
(2)使用應用程式期間允許訪問位置資料

 

 

- (void)requestWhenInUseAuthorization;
樣本如下:
locationManager=[[CLLocationManager alloc] init];    locationManager.delegate=self;    locationManager.desiredAccuracy=kCLLocationAccuracyBest;    locationManager.distanceFilter=10;    if (iOSVersion>=8) {        [locationManager requestWhenInUseAuthorization];//使用程式其間允許訪問位置資料(iOS8定位需要)    }    [locationManager startUpdatingLocation];//開啟定位
2、在Info.plist檔案中添加如下配置:
(1)NSLocationAlwaysUsageDescription
(2)NSLocationWhenInUseUsageDescription

 

這樣添加後,定位功能就能正常使用了!

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.