是否可以将用户输入的邮政编码转换为文本框并将其转换为CLLocation?我正在尝试比较其当前位置与地址或邮政编码之间的距离,如果可以从NSString中创建CLLocation,这将很容易。

最佳答案

该过程称为地理编码,如果实现该过程,它将是这样:

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必须正确添加到您的项目中。您可能需要处理最后一个完成块中的错误,我没有在上面的代码中添加此部分。

10-06 13:26
查看更多