CLGeocoder 根据地理位置获取经纬度,根据经纬度获取地理位置

该博客介绍了如何在iOS应用中使用CLGeocoder类进行地理位置与经纬度之间的转换。通过实现`getAddress`和`getDegree`两个方法,展示了如何从经纬度获取地理位置信息,以及如何从地址获取经纬度坐标。
部署运行你感兴趣的模型镜像
//
//  ViewController.m
//  LocationGenerateCoder
//
//  Created by hq on 16/5/17.
//  Copyright  2016年 hanqing. All rights reserved.
//

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

@interface ViewController ()

//编码地理位置用的类
@property(nonatomic,strong) CLGeocoder *geo;

@property (weak, nonatomic) IBOutlet UITextView *addressTextView;

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

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

- (IBAction)getAddress;

- (IBAction)getDegree;

@end

@implementation ViewController

-(CLGeocoder *)geo{
    
    if (_geo==nil) {
        _geo=[[CLGeocoder alloc] init];
    }
    return _geo;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    

}

//将经纬度转换为我们的地理位置
- (IBAction)getAddress {
    
    CLLocationDegrees lat=[self.latitude.text doubleValue];
    CLLocationDegrees lon=[self.longtitude.text doubleValue];
    
    if ([self.latitude.text length]==0||[self.latitude.text length]==0) {
        NSLog(@"请输入经纬度");
        return;
    }
    
    CLLocation *loc=[[CLLocation alloc]initWithLatitude:lat longitude:lon];

    [self.geo reverseGeocodeLocation:loc completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
       
        if (error) {
            NSLog(@"出错了");
            return ;
        }
        
        for (CLPlacemark *mark in placemarks) {
            
            //获取省
            NSString *state=mark.administrativeArea;
            
            //获取城市
            NSString  *city=mark.locality;
            
            //街道名
            NSString *thoroughfare=mark.thoroughfare;
            
            NSLog(@"%@%@,%@",state,city,thoroughfare);
            
        }
    }];
}

//将地理位置转换为经纬度
- (IBAction)getDegree {
    
    NSString *inputAddress=self.addressTextView.text;
    
    if ([inputAddress length]==0) {
        NSLog(@"请输入正确的地址");
        return;
    }
    
    [self.geo geocodeAddressString:inputAddress completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        
        for (CLPlacemark *mark in placemarks) {
            
            CLLocation *loc=mark.location;
            
            //获取经纬度了
            self.latitude.text=@(loc.coordinate.latitude).stringValue;
            self.longtitude.text=@(loc.coordinate.longitude).stringValue;
            
        }
    }];
    
    /**该方法可以固定一个范围内查找,防止重名的情况
    [self.geo geocodeAddressString:@"北京大学" inRegion:(nullable CLRegion *)
     completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
     
     }];
     */
    
}

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



@end

您可能感兴趣的与本文相关的镜像

Seed-Coder-8B-Base

Seed-Coder-8B-Base

文本生成
Seed-Coder

Seed-Coder是一个功能强大、透明、参数高效的 8B 级开源代码模型系列,包括基础变体、指导变体和推理变体,由字节团队开源

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值