CLGeocoder基本使用

////  02-地理编码
//
//  Created by apple on 14-8-7.
//  Copyright (c) 2014年 CoderJee. All rights reserved.
//

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

@interface HMViewController ()
@property (nonatomic, strong) CLGeocoder *geocoder;

#pragma mark - 地理编码
- (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;

#pragma mark - 反地理编码
- (IBAction)reverseGeocode;
@property (weak, nonatomic) IBOutlet UITextField *longtitudeField;
@property (weak, nonatomic) IBOutlet UITextField *latitudeField;
@property (weak, nonatomic) IBOutlet UILabel *reverseDetailAddressLabel;
@end

@implementation HMViewController

- (CLGeocoder *)geocoder
{
    if (!_geocoder) {
        self.geocoder = [[CLGeocoder alloc] init];
    }
    return _geocoder;

}

 - (void)viewDidLoad
{
    [super viewDidLoad];
    
}

/**
 *  地理编码:地名 -> 经纬度
 */
- (void)geocode
{
    // 1.获得输入的地址xx
    NSString *address = self.addressField.text;
    if (address.length == 0) return;
    
    // 2.开始编码
    [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];
        }
    }];
}

/**
 *  反地理编码:经纬度 -> 地名
 */
- (void)reverseGeocode
{
    NSString *longtitudeText = self.longtitudeField.text;
    NSString *latitudeText = self.latitudeField.text;
    if (longtitudeText.length == 0 || latitudeText.length == 0) return;
    
    CLLocationDegrees latitude = [latitudeText doubleValue];
    CLLocationDegrees longtitude = [longtitudeText doubleValue];
    
    // 开始反向编码
    CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longtitude];
    [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        if (error || placemarks.count == 0) {
            self.reverseDetailAddressLabel.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.reverseDetailAddressLabel.text = firstPlacemark.name;
            
            CLLocationDegrees latitude = firstPlacemark.location.coordinate.latitude;
            CLLocationDegrees longitude = firstPlacemark.location.coordinate.longitude;
            self.latitudeField.text = [NSString stringWithFormat:@"%.2f", latitude];
            self.longtitudeField.text = [NSString stringWithFormat:@"%.2f", longitude];
        }
    }];

}

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

@end

 

转载于:https://www.cnblogs.com/Fc-ios/p/3945678.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值