从地址字符串中获取地址部分

时间:2013-07-06 01:50:42

标签: algorithm

我有这样的德国地址

Birkenweg 13 51491 Overath
Emil-Hoffmann-Str. 13 50996 Köln
Rolandstr. 65 50677 Köln
Markusplatz 35 50968 Köln
Bonner Str. 245 50968 Köln
Aachener Str. 1590 50858 Köln
Viersener Str. 122 50733 Köln
Bergische Landstr. 219 51375 Leverkusen
Josef Gladbach Platz 81-100 50259 Pulheim
Hauptstr. 769 51399 Burscheid
Bielsteiner Str. 117-119 51674 Wiehl

而在

Birkenweg 13 51491 Overath

我想对这些字段进行排序

Birkenweg = street
13 = house number
51491 = postcode
Overath = city

并在

Emil-Hoffmann-Str. 13 50996 Köln

它的

Emil-Hoffmann-Str. = street
13 = house number
50996 = postcode
Köln = city

并在

Josef Gladbach Platz 81-100 50259 Pulheim

它的

Josef Gladbach Platz = street
81-100 = house number
50259 = postcode
Pulheim = city

您建议采用哪种可靠方式?

2 个答案:

答案 0 :(得分:2)

如果您有坐标,请

Google Reverse Geocoding API

如果您有地址,请

Google Geocoding API

答案 1 :(得分:1)

搜索5个连续数字(因为这似乎是最可靠的搜索内容),例如使用正则表达式。这会给你邮政编码和城市。在那里拆分字符串。在邮政编码之前搜索字符串末尾的门牌号码。请注意,它可能包含字母,例如“Habsburgerstrasse 55a”。

与此类似:

function splitGermanAddress(s) {
    var result = {};
    var parts = s.split(/\d\d\d\d\d/);
    var address = parts[0];
    result.postcode = s.substr(address.length, 5);
    result.city = parts[1].trim();

    var houseNo = /\d+\s*[a-z]?(\s*\-\s*\d+\s*[a-z]?)?\s*$/i
    var match = address.match(houseNo);
    if (match) {
        result.houseNumber = match[0].trim();
        result.street = address.substr(
            0, address.length - match[0].length).trim();
    } else {
        result.street = address;
    }
    return result;
}

演示:http://jsfiddle.net/99Lzm/2/