【问题标题】:How to convert an address string to an address dictionary如何将地址字符串转换为地址字典
【发布时间】:2014-05-13 15:14:45
【问题描述】:

我使用 OSX 数据检测器从文本中提取地址。这给了我像“1 Infinite Loop, Cupertino, CA”这样的地址字符串。
为了将这些地址放入 OSX 通讯簿,我必须将它们转换为带有“Street”、“City”、“Country”等键的字典”等
我知道有一个相反的功能:ABCreateStringWithAddressDictionary(...),但我需要它。
我知道可以做到这一点的唯一方法(当然,这完全是矫枉过正)是将地址字符串发送到CLGeocoder,它返回地址的坐标,然后再次发送坐标以进行反向地理编码,这返回包含地址字典的地标。
必须有更简单的方法来做到这一点。
编辑:
我现在学会了,
1)这个问题真的很复杂,并且通过使用地理编码服务器确实做得最好,参见例如here,
2)只联系一次地理编码服务器就足够了:当在那里发送地址字符串时,会发送回地址目录。

【问题讨论】:

    标签: macos geocoding addressbook


    【解决方案1】:

    如果您使用的是NSTextCheckingTypeAddress 类型的数据检测器,则返回的匹配项应提供addressComponents 字典。
    该字典包含您提到的所有键。

    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);
        }
    }
    

    上面的 sn -p 为输入字符串返回以下内容:

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

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

    NSTextCheckingNameKey
    NSTextCheckingJobTitleKey
    NSTextCheckingOrganizationKey
    NSTextCheckingStreetKey
    NSTextCheckingCityKey
    NSTextCheckingStateKey
    NSTextCheckingZIPKey
    NSTextCheckingCountryKey
    NSTextCheckingPhoneKey
    NSTextCheckingAirlineKey
    NSTextCheckingFlightKey
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-15
      • 1970-01-01
      • 2011-08-02
      • 1970-01-01
      • 2020-10-22
      • 1970-01-01
      • 2020-08-25
      相关资源
      最近更新 更多