导入头文件 #import
pragma -mark 不用代理 协议
(CLGeocoder *)geocoder{
if(_geocoder){
_geocoder = [[CLGeocoder alloc]init];
}
return _geocoder;
}
3.地理编码 ========= 地名==经纬度
NSString *address = _location.text;
if([address length]== 0){
return;
}
[self.geocoder geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if(error == nil){
[placemarks enumerateObjectsUsingBlock:^(CLPlacemark * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
self.location.text = obj.name;
self.longitude.text = @(obj.location.coordinate.longitude).stringValue;
self.latitude.text = @(obj.location.coordinate.latitude).stringValue;
}];
}
}];
4.反地理编码 ========== 经纬度==地名
CGFloat longitude = [_longitude.text doubleValue];
CGFloat latitude = [_latitude.text doubleValue];
CLLocation *location = [[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if(error == nil){
[placemarks enumerateObjectsUsingBlock:^(CLPlacemark * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
self.location.text = obj.name;
self.longitude.text = @(obj.location.coordinate.longitude).stringValue;
self.latitude.text = @(obj.location.coordinate.latitude).stringValue;
}];
}
}];