ios學習筆記之地圖(中)

一 前言

本章主要介紹地理編碼及反地理編碼,需要用到CLGeocoder類

二 地理編碼

在下圖綠框內輸入地址,點擊地理編碼,顯示經度和緯度

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextView *addressTV;// 那個綠框textView
@property (weak, nonatomic) IBOutlet UITextField *longTF;//經度的field
@property (weak, nonatomic) IBOutlet UITextField *laTF;//緯度的field
//地理編碼
@property (nonatomic, strong) CLGeocoder *geoC;
@end

@implementation ViewController

#pragma mark - 懶加載
- (CLGeocoder *)geoC{
    if (!_geoC) {
        _geoC = [[CLGeocoder alloc] init];
    }
    
    return _geoC;
}

//點擊“地理編碼”按鈕
- (IBAction)geoCode {
    
    NSString *str = self.addressTV.text;
    
    if ([str length] == 0) {
        return ;
    }
    
    
//    編碼其實是去向蘋果服務器請求數據
    [self.geoC geocodeAddressString:str completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        /**
         * CLPlacemark
         *
         */
        
        if (!error) {
            NSLog(@"%@",placemarks);
//            遍歷一下這個數組,因為可能有多個查詢結果被返回
            [placemarks enumerateObjectsUsingBlock:^(CLPlacemark * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                NSLog(@"name = %@",obj.name);
                self.addressTV.text = obj.name;
//                self.laTF.text = [NSString stringWithFormat:@"%f",obj.location.coordinate.latitude];
//                self.longTF.text = [NSString stringWithFormat:@"%f",obj.location.coordinate.longitude];
                self.laTF.text = @(obj.location.coordinate.latitude).stringValue;//獲obj.location.coordinate.latitude的字符串格式
                self.longTF.text = @(obj.location.coordinate.longitude).stringValue;
                
            }];
            
        }else{
            NSLog(@"error = %@",error);
        }
    }];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

@end
三 反地理編碼

在上圖輸入經度和緯度的地方,分別輸入經度和緯度,點擊反地理編碼,在綠框內顯示地址

//點擊“反地理編碼”按鈕
- (IBAction)reverseGeoCode {
    
    CLLocationDegrees latitude = [self.laTF.text doubleValue];
    CLLocationDegrees longtitude = [self.longTF.text doubleValue];
    
//    TODO: 容錯,防止用戶輸入非經緯度的內容
    
//    if (!) {
//        <#statements#>
//    }
    
    CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longtitude];
    [self.geoC reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        if (!error) {
            
            NSLog(@"%@",placemarks);
            [placemarks enumerateObjectsUsingBlock:^(CLPlacemark * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                self.addressTV.text = obj.name;
            }];
            
            
        }else{
            NSLog(@"無法正常編碼");
        }
        
        
    }];
    
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 一、簡單說明 CLGeocoder:地理編碼器,其中Geo是地理的英文單詞Geography的簡寫。 1.使用CL...
    行走的菜譜閱讀 589評論 0 0
  • 出自http://my.oschina.net/are1OfBlog/blog/420034 摘要 現在很多社交、...
    JJO閱讀 4,165評論 4 19
  • http://www.cnblogs.com/kenshincui/p/4125570.html 摘要: 現在很多...
    大崔老師閱讀 3,314評論 1 2
  • *初學地圖時,覺得地圖是個很高深的玩意兒,導航、定位、檢索這得運用多少算法和核心動畫的知識點啊,于是一直排斥 * ...
    柳駿閱讀 3,860評論 11 22
  • 夜,深邃如墨, 微弱的月光撒在頭頂,伴著風和云。 我想拾起一縷這般驚心動魄的皎潔, 它卻從指尖里溜走。 原來美的如...
    姓名閱讀 408評論 0 8