一、開場白
身處移動互聯網時代,App已經能幫我們打理生活中的大小事,比如導航去陌生的地方,或者查找周邊的餐館、酒店、銀行與電影院。這些功能的背後,離不開地圖與定位兩大能力。在iOS裡想要用上它們,通常要依托兩個框架:負責地圖展示的Map Kit,以及負責地理定位的Core Location。
順帶一提兩個常見術語:LBS,即Location Based Service,基於位置的服務;SoLoMo,也就是Social Local Mobile,社交、本地與移動的結合。
二、CoreLocation框架的使用
1.使用前提
(1)導入框架。從Xcode 5之後,這個框架已無需手動導入。

(2)導入主頭文件:
#import <CoreLocation/CoreLocation.h>
2.使用須知
CoreLocation框架中的所有數據類型都以CL為前綴,而用戶定位則由CLLocationManager對象來完成。
三、經緯度常識掃盲

本初子午線穿過英國倫敦的格林文治天文台,向東為東經(E),向西為西經(W),東西經各180°,合計360°。赤道則是零度緯線,向北為北緯(N),向南為南緯(S),南北緯各90°,合計180°。
小提示:所跨越的經緯度越大(1°約合111公里),代表范圍越廣,地圖上的內容也就顯得越小。
4.我國的經緯度范圍
緯度范圍:N 3°51′ ~ N 53°33′
經度范圍:E 73°33′ ~ E 135°05′
部分城市的經緯度可參考下圖:

四、模擬位置
測試程序時,可以在模擬器上設定虛擬的經緯度坐標,方便驗證定位效果。


CoreLocation地理編碼
CLGeocoder是核心類,Geo取自Geography(地理)。它能完成兩件事:
地理編碼:由地名換算出經緯度、完整地址等具體信息;
反地理編碼:由經緯度反推出具體的位置信息。
(1)地理編碼方法:
- (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;
(2)反地理編碼方法:
- (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;
2.CLGeocodeCompletionHandler
無論編碼成功或失敗,完成後都會回調這個block,它傳遞兩個參數: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文件
#import "YYViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface YYViewController ()
@property(nonatomic,strong)CLGeocoder *geocoder;
- (IBAction)geocode;
@property (weak, nonatomic) IBOutlet UITextField *addressField;
@property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *detailAddressLabel;
- (IBAction)reverseGeocode;
@property (weak, nonatomic) IBOutlet UITextField *longitudeField;
@property (weak, nonatomic) IBOutlet UITextField *latitudeField;
@property (weak, nonatomic) IBOutlet UILabel *reverdeDetailAddressLabel;
@end
@implementation YYViewController
//懶加載
-(CLGeocoder *)geocoder
{
if (_geocoder==nil) {
_geocoder=[[CLGeocoder alloc]init];
}
return _geocoder;
}
//地理編碼:地名 → 經緯度
- (IBAction)geocode {
NSString *address=self.addressField.text;
if (address.length==0) return;
[self.geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
if (error || placemarks.count==0) {
self.detailAddressLabel.text=@"你輸入的地址沒找到,可能在月球上";
}else{
for (CLPlacemark *placemark in placemarks) {
NSLog(@"name=%@ locality=%@ country=%@ postalCode=%@",placemark.name,placemark.locality,placemark.country,placemark.postalCode);
}
CLPlacemark *firstPlacemark=[placemarks firstObject];
self.detailAddressLabel.text=firstPlacemark.name;
CLLocationDegrees latitude=firstPlacemark.location.coordinate.latitude;
CLLocationDegrees longitude=firstPlacemark.location.coordinate.longitude;
self.latitudeLabel.text=[NSString stringWithFormat:@"%.2f",latitude];
self.longitudeLabel.text=[NSString stringWithFormat:@"%.2f",longitude];
}
}];
}
//反地理編碼:經緯度 → 地名
- (IBAction)reverseGeocode {
NSString *longtitudeText=self.longitudeField.text;
NSString *latitudeText=self.latitudeField.text;
if (longtitudeText.length==0||latitudeText.length==0) return;
CLLocationDegrees latitude=[latitudeText doubleValue];
CLLocationDegrees longitude=[longtitudeText doubleValue];
CLLocation *location=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
if (error||placemarks.count==0) {
self.reverdeDetailAddressLabel.text=@"你輸入的地址沒找到,可能在月球上";
}else{
CLPlacemark *firstPlacemark=[placemarks firstObject];
self.reverdeDetailAddressLabel.text=firstPlacemark.name;
CLLocationDegrees latitude=firstPlacemark.location.coordinate.latitude;
CLLocationDegrees longitude=firstPlacemark.location.coordinate.longitude;
self.latitudeField.text=[NSString stringWithFormat:@"%.2f",latitude];
self.longitudeField.text=[NSString stringWithFormat:@"%.2f",longitude];
}
}];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
@end
實現效果如下:
(1)地理編碼(地名 → 經緯度):

打印輸出:

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

(3)點擊經緯度輸入框時,鍵盤彈出效果:


(4)需要留意的是,由於蘋果在中國境內的地圖服務商是高德地圖,因此搜索結果都限定在中國范圍內。