iOS开发中使用CoreLocation框架处理地理编码的方法

2020-01-14 18:05:38于海丽

 */
- (IBAction)reverseGeocode {
    //1.获得输入的经纬度
    NSString *longtitudeText=self.longitudeField.text;
    NSString *latitudeText=self.latitudeField.text;
    if (longtitudeText.length==0||latitudeText.length==0) return;
    
    CLLocationDegrees latitude=[latitudeText doubleValue];
    CLLocationDegrees longitude=[longtitudeText doubleValue];
    
    CLLocation *location=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
    //2.反地理编码
    [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        if (error||placemarks.count==0) {
            self.reverdeDetailAddressLabel.text=@"你输入的地址没找到,可能在月球上";
        }else//编码成功
        {
            //显示最前面的地标信息
            CLPlacemark *firstPlacemark=[placemarks firstObject];
            self.reverdeDetailAddressLabel.text=firstPlacemark.name;
            //经纬度
            CLLocationDegrees latitude=firstPlacemark.location.coordinate.latitude;
            CLLocationDegrees longitude=firstPlacemark.location.coordinate.longitude;
            self.latitudeField.text=[NSString stringWithFormat:@"%.2f",latitude];
            self.longitudeField.text=[NSString stringWithFormat:@"%.2f",longitude];
        }
    }];
}

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


实现效果:

 

(1)地理编码:(地名->经纬度坐标)

iOS开发中使用CoreLocation框架处理地理编码的方法