在ios中,我们可以通过利用Core Location框架中的CLGeocoder去完成地理编码和反地理编码。
其中的地理编码就是通过给定的地名,获取具体的位置的信息
反地理编码就是根据给定的经纬度去获取具体的位置信息。
地理编码的应用如下
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()
//输入的地址
@property (weak, nonatomic) IBOutlet UITextField *addressField;
//输入的经度
@property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;
//输入的纬度
@property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;
//输出的详细的地址
@property (weak, nonatomic) IBOutlet UILabel *detailAddressLabel;
//设置地理编码的对象
@property (nonatomic ,strong)CLGeocoder *geocoder;
@end
@implementation ViewController
-(CLGeocoder *)geocoder
{
//懒加载创建地理编码的对象
if(_geocoder==nil)
{
_geocoder = [[CLGeocoder alloc]init];
}
return _geocoder;
}
- (IBAction)Mapcode:(id)sender {
//获取用户输入的位置
NSString * str = self.addressField.text;
if(str.length==0)
{
return;
}
//利用地理编码对象进行编码,根据传入的地址获取该地址对应的经纬度的信息
[self.geocoder geocodeAddressString:str completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks,
NSError * _Nullable error) {
if(placemarks.count == 0|| error == nil)
{
return;
}
//placemarks是地标数组,地标数组中存放着地标,每一个地标包含了这个位置的经纬度和城市以及区域和国家的名字等信息
//获取第一个地标
CLPlacemark * placemark = [placemarks firstObject];
//placemark.name在这里是地址的名称,也就是你输入的
//在label中显示经度
self.longitudeLabel.text = [NSString stringWithFormat:@"%f",placemark.location.coordinate.longitude];
//在label中显示纬度
self.latitudeLabel.text = [NSString stringWithFormat:@"%f",placemark.location.coordinate.latitude];
//获取详细的地址信息,因为addressDictionary中的FormattedAddressLines,这个键对应的值是数组,存储详细信息,所以我们遍历它去取
NSArray * address = placemark.addressDictionary[@"FormattedAddressLines"];
NSMutableString * strM = [NSMutableString string];
for(NSString * str in address)
{
[strM appendString:str];
}
//设置给详细label去显示
self.detailAddressLabel.text = strM;
NSLog(@"%@",placemark.region);
NSLog(@"-----------");
//所在的城市
NSLog(@"%@",placemark.locality);
}];
}
地理的反编码
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()
//纬度
@property (weak, nonatomic) IBOutlet UITextField *latitudeLabel;
//经度
@property (weak, nonatomic) IBOutlet UITextField *longtitudeLabel;
//地址的详细信息
@property (weak, nonatomic) IBOutlet UILabel *reverseDetailLabel;
//地理编码的对象
@property (nonatomic ,strong)CLGeocoder *geocoder;
@end
@implementation ViewController
-(CLGeocoder *)geocoder
{
if(_geocoder==nil)
{
_geocoder = [[CLGeocoder alloc]init];
}
return _geocoder;
}
- (IBAction)reverseGeocode:(id)sender {
//1、获取用户输入的经纬度
NSString * longtitude = self.longtitudeLabel.text;
NSString * latitude = self.latitudeLabel.text;
if(longtitude.length == 0||latitude.length==0)
{
NSLog(@"请输入经纬度");
return;
}
//2、根据用户输入的经纬度去创建CLLocation对象
CLLocation * location = [[CLLocation alloc]initWithLatitude:[longtitude doubleValue] longitude:[latitude doubleValue]];
//3、根据location去获取对应的地标信息
[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks,
NSError * _Nullable error) {
// for (CLPlacemark * placemark in placemarks)
// {
// NSLog(@"%@ %@ %f %f",placemark.name,placemark.addressDictionary,placemark.location.coordinate.longitude,placemark.location.coordinate.latitude);
// }
CLPlacemark * placeMark = [placemarks firstObject];
self.reverseDetailLabel.text = placeMark.locality;
}];
}
具体运用步骤:
正地理编码:
1、创建一个CLGeocoder的对象
2、实现地理编码的方法
3、遍历数组,然后获取数据
反地理编码:
1、创建一个CLGeocoder对象
2、创建一个CLLocation对象
3、实现反地理编码方法
4、遍历数组,获取数据
再补充下,如果横跨的经度\纬度越大的话,表示的范围就越大,在地图上看到的东西就越小。
在赤道上经度差1度对应的实际距离是111千米;
在经线上纬度差1度对应的实际距离是111千米;
在除赤道外的其他纬线上,经度差1度对应的实际距离是111*cos纬度。