一、CLGeocoder 地理编码 与 反地理编码
-
地理编码
:- 根据给定的地名,获得具体的位置信息(比如经纬度、地址的全称等)
// 地理编码方法 -(void)geocodeAddressString:(NSString*)addressStringcompletionHandler:(CLGeocodeCompletionHandler)completionHandler;
-
反地理编码
:- 根据给定的经纬度,获得具体的位置信息
// 反地理编码方法
-(void)reverseGeocodeLocation:(CLLocation*)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;
+ 注意:CLGeocodeCompletionHandler
- 当地理\反地理编码完成时,就会`调用CLGeocodeCompletionHandler`,可以`获取到CLPlacemark对象`
```objc
// 这个block传递2个参数
// error:当编码出错时(比如编码不出具体的信息)有值
// placemarks:里面装着CLPlacemark对象
typedef void(^CLGeocodeCompletionHandler)
(NSArray*placemarks, NSError*error);
- CLPlacemark(locality:城市名称 thoroughfare:街道名称 name:全称 CLLocation *location)
二、应用场景
- 1、与定位结合使用,用于确定当前用户所在的具体地址信息
三、实例
#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
网友评论
[geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (!error) {
NSLog(@"sucesss");
}else{
NSLog(@"%@",error);
}
Ps:在忽视手机默认语言的情况下,及时 手机语言是英语, 都显示汉语,
/**
* 逆地理解析
*/
- (void)searchReGeocodeWithCoordinate:(CLLocationCoordinate2D)coordinate
{
NSLog(@"开始逆地理解析");
AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
regeo.location = [AMapGeoPoint locationWithLatitude:coordinate.latitude
longitude:coordinate.longitude];
regeo.requireExtension = YES;
[self.search AMapReGoecodeSearch:regeo];
NSLog(@"逆地理解析结束");
}
#pragma mark - AMapSearchDelegate
// 请求错误时, 调用此代理方法
- (void)AMapSearchRequest:(id)request didFailWithError:(NSError *)error {
NSLog(@"%s: searchRequest = %@, errInfo= %@", __func__, [request class], error);
NSLog(@"逆地理解析错误时回调");
if ([request isKindOfClass:[AMapReGeocodeSearchRequest class]])
{
NSLog(@"位置解析失败");
// [self showMessage:@"位置解析失败"];
RecordAdd1Cell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
cell.descLabel.text = @"未能有效获取当前位置信息";
}
}
// 逆地理编码回调方法
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response {
NSLog(@"逆地理解析回调方法");
if (response.regeocode != nil)
{
NSLog(@"逆地理编码");
NSString *dizhi = response.regeocode.formattedAddress;
NSLog(@"dizhi:%@", dizhi);
NSString *localString = @"未能有效获取当前位置信息";
if (dizhi.length == 0) {
dizhi = localString;
}
RecordAdd1Cell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
cell.descLabel.text = dizhi;
}
}
如何获取到跟android定位sdk里面一样的citycode?
String s1 = location.getCityCode();
那么iOS的呢