码迷,mamicode.com
首页 > 其他好文 > 详细

GeocodeLocation

时间:2016-01-17 20:06:26      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:

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

@interface ViewController ()


/** 地理编码 */
@property (nonatomic, strong) CLGeocoder *geoC;
@property (weak, nonatomic) IBOutlet UITextView *addressTV;

@property (weak, nonatomic) IBOutlet UITextField *latitudeTF;

@property (weak, nonatomic) IBOutlet UITextField *longitudeTF;
@end

@implementation ViewController


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


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

/**
 *  地理编码(地址转经纬度)
 */
- (IBAction)geoCoder {
    
    NSString *address = self.addressTV.text;
    
    // 容错
    if([address length] == 0)
        return;
    
    [self.geoC geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        // CLPlacemark : 地标
        // location : 位置对象
        // addressDictionary : 地址字典
        // name : 地址详情
        // locality : 城市
        
        if(error == nil)
        {
            CLPlacemark *pl = [placemarks firstObject];
            self.addressTV.text = pl.name;
            self.latitudeTF.text = @(pl.location.coordinate.latitude).stringValue;
            self.longitudeTF.text = @(pl.location.coordinate.longitude).stringValue;
        }else
        {
            NSLog(@"错误");
        }

        
    }];
    
    
}
- (IBAction)reverseGeoCoder {
    
    // 获取用户输入的经纬度
    double latitude = [self.latitudeTF.text doubleValue];
    double longitude = [self.longitudeTF.text doubleValue];
    
    CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
    
    // 反地理编码(经纬度---地址)
    [self.geoC reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if(error == nil)
        {
            CLPlacemark *pl = [placemarks firstObject];
            self.addressTV.text = pl.name;
            self.latitudeTF.text = @(pl.location.coordinate.latitude).stringValue;
            self.longitudeTF.text = @(pl.location.coordinate.longitude).stringValue;
        }else
        {
            NSLog(@"错误");
        }

    }];
    
    
}


@end

  

GeocodeLocation

标签:

原文地址:http://www.cnblogs.com/yedayi/p/5137683.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!