iOS開發拓展篇—CoreLocation地理編碼

來源:互聯網
上載者:User

iOS開發拓展篇—CoreLocation地理編碼
一、簡單說明 CLGeocoder:地理編碼器,其中Geo是地理的英文單詞Geography的簡寫。 1.使用CLGeocoder可以完成“地理編碼”和“反地理編碼” 地理編碼:根據給定的地名,獲得具體的位置資訊(比如經緯度、地址的全稱等) 反地理編碼:根據給定的經緯度,獲得具體的位置資訊   (1)地理編碼方法   - (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;  (2)反地理編碼方法   - (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;   2.CLGeocodeCompletionHandler   當地理\反地理編碼完成時,就會調用CLGeocodeCompletionHandler    這個block傳遞2個參數 error :當編碼出錯時(比如編碼不出具體的資訊)有值 placemarks :裡面裝著CLPlacemark對象   3.CLPlacemark 說明:CLPlacemark的字面意思是地標,封裝詳細的地址位置資訊 地理位置     @property (nonatomic, readonly) CLLocation *location;   地區       @property (nonatomic, readonly) CLRegion *region; 詳細的地址資訊   @property (nonatomic, readonly) NSDictionary *addressDictionary; 位址名稱    @property (nonatomic, readonly) NSString *name; 城市      @property (nonatomic, readonly) NSString *locality;   二、程式碼範例: 在storyboard中搭建介面如下:    實現代碼:   YYViewController.m檔案 複製代碼  1 //  2 //  YYViewController.m  3 //  19-地理編碼  4 //  5 //  Created by apple on 14-8-11.  6 //  Copyright (c) 2014年 yangyong. All rights reserved.  7 //  8   9 #import "YYViewController.h" 10 #import <CoreLocation/CoreLocation.h> 11  12 @interface YYViewController () 13 @property(nonatomic,strong)CLGeocoder *geocoder; 14 #pragma mark-地理編碼 15 - (IBAction)geocode; 16 @property (weak, nonatomic) IBOutlet UITextField *addressField; 17 @property (weak, nonatomic) IBOutlet UILabel *longitudeLabel; 18 @property (weak, nonatomic) IBOutlet UILabel *latitudeLabel; 19 @property (weak, nonatomic) IBOutlet UILabel *detailAddressLabel; 20  21 #pragma mark-反地理編碼 22  23 - (IBAction)reverseGeocode; 24 @property (weak, nonatomic) IBOutlet UITextField *longitudeField; 25 @property (weak, nonatomic) IBOutlet UITextField *latitudeField; 26 @property (weak, nonatomic) IBOutlet UILabel *reverdeDetailAddressLabel; 27 @end 28  29 @implementation YYViewController 30  31 #pragma mark-懶載入 32 -(CLGeocoder *)geocoder 33 { 34     if (_geocoder==nil) { 35         _geocoder=[[CLGeocoder alloc]init]; 36     } 37     return _geocoder; 38 } 39 - (void)viewDidLoad 40 { 41     [super viewDidLoad]; 42 } 43 /** 44  *  地理編碼:地名—>經緯度座標 45  */ 46 - (IBAction)geocode { 47     //1.獲得輸入的地址 48     NSString *address=self.addressField.text; 49     if (address.length==0) return; 50      51     //2.開始地理編碼 52     //說明:調用下面的方法開始編碼,不管編碼是成功還是失敗都會調用block中的方法 53     [self.geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) { 54         //如果有錯誤資訊,或者是數組中擷取的地名元素數量為0,那麼說明沒有找到 55         if (error || placemarks.count==0) { 56             self.detailAddressLabel.text=@"你輸入的地址沒找到,可能在月球上"; 57         }else   //  編碼成功,找到了具體的位置資訊 58         { 59             //列印查看找到的所有的位置資訊 60                 /* 61                     name:名稱 62                     locality:城市 63                     country:國家 64                     postalCode:郵遞區號 65                  */ 66             for (CLPlacemark *placemark in placemarks) { 67                 NSLog(@"name=%@ locality=%@ country=%@ postalCode=%@",placemark.name,placemark.locality,placemark.country,placemark.postalCode); 68             } 69              70             //取出擷取的地理資訊數組中的第一個顯示在介面上 71             CLPlacemark *firstPlacemark=[placemarks firstObject]; 72             //詳細位址名稱 73             self.detailAddressLabel.text=firstPlacemark.name; 74             //緯度 75             CLLocationDegrees latitude=firstPlacemark.location.coordinate.latitude; 76             //經度 77             CLLocationDegrees longitude=firstPlacemark.location.coordinate.longitude; 78             self.latitudeLabel.text=[NSString stringWithFormat:@"%.2f",latitude]; 79             self.longitudeLabel.text=[NSString stringWithFormat:@"%.2f",longitude]; 80         } 81     }]; 82 } 83  84 /** 85  *  反地理編碼:經緯度座標—>地名 86  */ 87 - (IBAction)reverseGeocode { 88     //1.獲得輸入的經緯度 89     NSString *longtitudeText=self.longitudeField.text; 90     NSString *latitudeText=self.latitudeField.text; 91     if (longtitudeText.length==0||latitudeText.length==0) return; 92      93     CLLocationDegrees latitude=[latitudeText doubleValue]; 94     CLLocationDegrees longitude=[longtitudeText doubleValue]; 95      96     CLLocation *location=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude]; 97     //2.反地理編碼 98     [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) { 99         if (error||placemarks.count==0) {100             self.reverdeDetailAddressLabel.text=@"你輸入的地址沒找到,可能在月球上";101         }else//編碼成功102         {103             //顯示最前面的地標資訊104             CLPlacemark *firstPlacemark=[placemarks firstObject];105             self.reverdeDetailAddressLabel.text=firstPlacemark.name;106             //經緯度107             CLLocationDegrees latitude=firstPlacemark.location.coordinate.latitude;108             CLLocationDegrees longitude=firstPlacemark.location.coordinate.longitude;109             self.latitudeField.text=[NSString stringWithFormat:@"%.2f",latitude];110             self.longitudeField.text=[NSString stringWithFormat:@"%.2f",longitude];111         }112     }];113 }114 115 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event116 {117     [self.view endEditing:YES];118 }119 @end

聯繫我們

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