地图

这里写图片描述

#import "ViewController.h"

@interface ViewController ()

@property(nonatomic, strong)CLLocationManager * locationManager;//位置管理者
@property (weak, nonatomic) IBOutlet UILabel *outputLongtude;//显示经度
@property (weak, nonatomic) IBOutlet UILabel *outputLatitude;//显示维度
@property (weak, nonatomic) IBOutlet UILabel *outputLocation;//显示输出信息
@property (weak, nonatomic) IBOutlet UITextField *inputlongtude;//输入经度
@property (weak, nonatomic) IBOutlet UITextField *inputLatitude;//输入维度
@property (weak, nonatomic) IBOutlet UILabel *displayLongitude;
@property (weak, nonatomic) IBOutlet UILabel *displayLaitude;

- (IBAction)locationCovertAction:(UIButton *)sender;

- (IBAction)geocoderConverAction:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *displayLocation;
@property (weak, nonatomic) IBOutlet UITextField *inputLocation;

@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //初始化位置管理者
    self.locationManager = [[CLLocationManager alloc]init];

    //开启定位

    [_locationManager startUpdatingLocation];

    //ios8之后如果想要定位的话,必须授权
    [_locationManager requestWhenInUseAuthorization];

    //绑定代理
    self.locationManager.delegate = self;




    // Do any additional setup after loading the view, typically from a nib.
}
//实现协议中的方法,位置跟新走这个代理方法
- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations
{
//    NSLog(@"%@",locations);

    CLLocation * laoction  = locations.firstObject;

    //打印经纬度
//    
//    NSLog(@"%f",laoction.coordinate.latitude);
//    NSLog(@"%f",laoction.coordinate.longitude);
//    
    self.outputLongtude.text = [NSString stringWithFormat:@"%f",laoction.coordinate.longitude];
    self.outputLatitude.text = [NSString stringWithFormat:@"%f",laoction.coordinate.latitude];
//    self.outputLocation.text = [NSString stringWithFormat:@"%@",laoction];

    //地理编码  反地理编码
    CLGeocoder * geo = [[CLGeocoder alloc]init];

    //反地理编码(把经纬度转化为位置信息)
    [geo reverseGeocodeLocation:laoction completionHandler:^(NSArray *placemarks, NSError *error) {

        CLPlacemark * mark = placemarks.firstObject;

        self.outputLocation.text = mark.name;

//        NSLog(@"%@",mark.name);
    }];

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

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//根据经纬度 转换 地理信息


- (IBAction)locationCovertAction:(UIButton *)sender {



    //地理编码
    CLGeocoder * geo = [[CLGeocoder alloc]init];

    //修饰弱引用
    __weak ViewController * VC = self;

    //根据输入的地理位置进行编码
    [geo geocodeAddressString:self.inputLocation.text completionHandler:^(NSArray *placemarks, NSError *error) {

        CLPlacemark * mark = placemarks.firstObject;

        //获取经纬度
        VC.displayLaitude.text = [NSString stringWithFormat:@"%f",mark.location.coordinate.latitude];
        VC.displayLongitude.text = [NSString stringWithFormat:@"%f",mark.location.coordinate.longitude];

    }];

}

- (IBAction)geocoderConverAction:(id)sender {

    //根据输入的经纬度获取地理位置
    CLLocation * location = [[CLLocation alloc]initWithLatitude:[self.inputLatitude.text doubleValue] longitude:[self.inputlongtude.text doubleValue]];

    //地理编码
    CLGeocoder * geo = [[CLGeocoder alloc]init];

    //根据经纬度转换地理位置
    [geo reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {

        CLPlacemark * mark = placemarks.firstObject;

        self.displayLocation.text = mark.name;
    }];

}
@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值