如何将地址字符串转换为地址字典 [英] How to convert an address string to an address dictionary

查看:96
本文介绍了如何将地址字符串转换为地址字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用OSX数据检测器从文本中提取地址.这给了我地址字符串,例如" 1 Infinite Loop,Cupertino,CA ".
为了将这样的地址放入OSX通讯簿中,我必须将它们转换为带有"街道","城市","国家/地区"之类的键的字典. "等.
我知道有一个函数可以执行相反的操作:ABCreateStringWithAddressDictionary(...),但是我需要另一种方法.
我知道可以做到这一点的唯一方法(当然,这完全是彻底的过大)是将地址字符串发送到CLGeocoder,它返回地址的坐标,然后再次发送坐标以进行反向地理编码,返回包含地址字典的地标.
必须有一种更简单的方法来做到这一点.
编辑:
我现在知道了,
1)这个问题确实很复杂,并且实际上最好通过使用地理编码服务器来解决,请参见例如此处
2)只需联系一次地理编码服务器就足够了:当地址字符串发送到该地址字符串时,地址目录将被发回.

I extract addresses from text using OSX data detectors. This gives me address strings like "1 Infinite Loop, Cupertino, CA".
In order to put such addresses into the OSX address book, I have to convert them to a dictionary with keys like "Street", "City", "Country" etc.
I know that there is a function that does the reverse: ABCreateStringWithAddressDictionary(...), but I need it the other way.
The only way I know this can be done (and is, of course, total overkill) is to send the address string to a CLGeocoder, which returns the coordinates of the address, and send then the coordinates again for reverse geocoding, which returns a placemark containing the address dictionary.
There must be an easier way to do this.
EDIT:
I learned by now,
1) that this problem is really complicated, and is really done best by using a geocoding server, see e.g. here,
2) that it is enough to contact the geocoding server only once: When an address string is sent there, an address directory is sent back.

推荐答案

如果您正在使用类型为NSTextCheckingTypeAddress的数据检测器,则返回的匹配项应提供

If you are using a data detector with type NSTextCheckingTypeAddress, the returned matches should provide an addressComponents dictionary.
That dictionary contains all the keys you mentioned.

NSDataDetector* detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeAddress error:nil];
NSString* addressString = @"Apple, 1 Infinite Loop, Cupertino, CA";
NSArray* matches = [detector matchesInString:addressString options:0 range:NSMakeRange(0, addressString.length)];
for(NSTextCheckingResult* match in matches)
{
    if(match.resultType == NSTextCheckingTypeAddress)
    {
        NSDictionary* addressComponents = [match addressComponents];
        NSLog(@"Address Dictionary:%@", addressComponents);
    }
}

上面的代码段为输入字符串返回以下内容:

The above snippet returns the following for the input string:

Address Dictionary:{
    City = Cupertino;
    State = CA;
    Street = "1 Infinite Loop";
}

功能齐全的地址字符串将产生以下键的值:

A fully featured address string would result in values for the following keys:

NSTextCheckingNameKey
NSTextCheckingJobTitleKey
NSTextCheckingOrganizationKey
NSTextCheckingStreetKey
NSTextCheckingCityKey
NSTextCheckingStateKey
NSTextCheckingZIPKey
NSTextCheckingCountryKey
NSTextCheckingPhoneKey
NSTextCheckingAirlineKey
NSTextCheckingFlightKey

这篇关于如何将地址字符串转换为地址字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆