地址/邮政编码为NSLtring到CLLocation

时间:2014-06-19 16:19:04

标签: ios cllocationmanager cllocation

有没有办法将用户输入的邮政编码转换为文本框并将其转换为CLLocation?我试图比较它们当前位置与地址或邮政编码之间的距离,如果我可以从NSString中创建一个CLLocation,这将很容易。

1 个答案:

答案 0 :(得分:3)

称为地理编码的过程,如果您实现它,它就是这样的:

NString *_address = // any address or postcode

CLGeocoder *_geocoder = [[CLGeocoder alloc] init];
[_geocoder geocodeAddressString:_address completionHandler:^(NSArray *placemarks, NSError *error) {
    if (placemarks.count > 0) {
        CLPlacemark *_placemark = [placemarks firstObject];
        CLLocation *_location = _placemark.location;
        // ... do whaterver you want to do with the location
    }
}];

注意: CoreLocation.framework 必须正确添加到您的项目中。您可能需要处理最终完成块中的错误,我没有在上面的代码中添加这些部分。