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


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 *)event
116 {
117     [self.view endEditing:YES];
118 }
119 @end

 

實現效果:

(1)地理編碼:(地名->經緯度坐標)

  

打印輸出:

  

(2)反地理編碼:(經緯度—>地名)

   

(3)注意:調整鍵盤

  

  點擊經緯度textField進行輸入的時候,彈出的鍵盤如下

  

(4)注意:搜索的所有結果都是在中國境內的,因為蘋果在中國的地圖服務商是高德地圖。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM