iOS開發之定位擷取當前所在經緯度,進而獲得具體地址(省市地區街道)

來源:互聯網
上載者:User


前言:今天做iOS項目的時候,需要通過定位來拿到當期城市的名稱。百度地圖SDK有這個功能,但為了不依賴第三方,這裡我用iOS內建架構CoreLocation來實現這個需求。iOS8出來之後,針對定位需要多一點處理,才可以正常定位,這點會在文章末尾部分作出補充,在聲明補充之前的部分都是預設iOS7處理。

首先,我們需要在工程中匯入CoreLocation系統架構。然後在我們的控制器中引入標頭檔。

#import <CoreLocation/CoreLocation.h>

然後,聲明一個CLLocationManager對象作為成員變數,用於定位擷取經緯度座標,並遵守協議CLLocationManager的協議。

@interface TYViewController () <CLLocationManagerDelegate> {
    CLLocationManager *_locationManager;
}

實現其中的代理方法。

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    //將經度顯示到label上
    self.longitude.text = [NSString stringWithFormat:@"%lf", newLocation.coordinate.longitude];
    //將緯度現實到label上
    self.latitude.text = [NSString stringWithFormat:@"%lf", newLocation.coordinate.latitude];
 
    // 擷取當前所在的城市名
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    //根據經緯度反向地理編譯出地址資訊
    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *array, NSError *error){
         if (array.count > 0){
             CLPlacemark *placemark = [array objectAtIndex:0];
             //將獲得的所有資訊顯示到label上
             self.location.text = placemark.name;
             //擷取城市
             NSString *city = placemark.locality;
             if (!city) {
                 //四大直轄市的城市資訊無法通過locality獲得,只能通過擷取省份的方法來獲得(如果city為空白,則可知為直轄市)
                 city = placemark.administrativeArea;
             }
             NSLog(@"city = %@", city);
             _cityLable.text = city;
             [_cityButton setTitle:city forState:UIControlStateNormal];
         }
         else if (error == nil && [array count] == 0)
         {
             NSLog(@"No results were returned.");
         }
         else if (error != nil)
         {
             NSLog(@"An error occurred = %@", error);
         }
     }];
    //系統會一直更新資料,直到選擇停止更新,因為我們只需要獲得一次經緯度即可,所以擷取之後就停止更新
    [manager stopUpdatingLocation];
}

最後在viewDidLoad中初始化定位管理器。


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initializeLocationService];
    // Do any additional setup after loading the view.
}
- (void)initializeLocationService {
    // 初始化定位管理器
    _locationManager = [[CLLocationManager alloc] init];
    // 設定代理
    _locationManager.delegate = self;
    // 設定定位精確度到米
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    // 設定過濾器為無
    _locationManager.distanceFilter = kCLDistanceFilterNone;
    // 開始定位
    [_locationManager startUpdatingLocation];
}

接下來編譯並運行程式,手動在iOS模擬器上配置定位座標,即可列印出該經緯度座標組應的城市名(真機可直接列印出當前城市名)。

CLPlacemark類中封裝了很多的地理資訊屬性,包括完整的地址(大到國家,小到街道)和地理名稱等等,可以酌情使用。

PS:如果定位出的街道資訊為英文,那請先處理本地化,同時模擬器調整語言為中文。

以下為針對iOS8的補充:


如果需要正常定位,相對iOS7而言,iOS8需要額外處理兩個地方。

1. 工程的plist檔案裡面添加兩個欄位:NSLocationAlwaysUsageDescription,
NSLocationWhenInUseUsageDescription,type類型均為string,值可以根據你的需要填寫(也可以不填),填寫的內容會展示在APP提示使用者是否允許定位的alert內容裡面,具體效果可以自行測試,這裡就不額外截圖。

2. 調用定位方法之前需要額外調用一個函數,直接在上面iOS7初始化定位服務的方法裡面修改即可,具體如下:


- (void)initializeLocationService {
// 初始化定位管理器
_locationManager = [[CLLocationManager alloc] init];
// 設定代理
_locationManager.delegate = self;
// 設定定位精確度到米
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
// 設定過濾器為無
_locationManager.distanceFilter = kCLDistanceFilterNone;
// 開始定位
// 取得定位許可權,有兩個方法,取決於你的定位使用方式
// 一個是requestAlwaysAuthorization,一個是requestWhenInUseAuthorization
[_locationManager requestAlwaysAuthorization];//這句話ios8以上版本使用。
[_locationManager startUpdatingLocation];
}
這個需要真機測試,由於不會用蘋果手機截圖,所以就不上效果圖了,如果有興趣可以將代碼直接複製使用,自行運行一下看效果。

相關文章

聯繫我們

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