从邮政编码中找到完整的地址,解决方案是什么?


Finding full address from postcode, what is the solution?

基本上我正在为我的网站制作一个申请表格。我需要搜索使用用户输入邮政编码的完整地址,并希望提供该邮政编码的结果供他们选择。我知道,各种各样的数据库将需要,但我正在努力寻找来源,并将感谢任何帮助。

在英国,您可以使用192.com从任何邮政编码获取完整地址。

他们在192.com有一个完全免费的数据库

我不为他们或他们的任何广告商工作,但已经使用这个网站的许多数据输入应用程序。

根据您正在搜索的邮政编码格式化URL。例如

'a1 1aa' = http://www.192.com/places/a/a1-1/a1-1aa

这将列出邮政编码中的所有地址。

这是我写的类,希望它是有帮助的:

    Imports System.Net
    Imports System.IO
    Public Class PCLookup
        Property Addresses As List(Of Address)
        Public Sub New(Postcode As String)
            GetAddresses(CreatDoc(Postcode))
        End Sub
        Private Function CreatDoc(PostCode As String) As mshtml.HTMLDocument
            Dim URL As String = FormatPostcode(PostCode)
            If URL = "" Then Return New mshtml.HTMLDocument
            Dim request As HttpWebRequest = WebRequest.Create(URL)
            Dim response As HttpWebResponse = request.GetResponse()
            Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
            Dim doc As New mshtml.HTMLDocument
            Dim objDoc As mshtml.IHTMLDocument2 = doc
            Dim param As Object() = {reader.ReadToEnd()}
            objDoc.write(param)
            response.Close()
            reader.Close()
            Return objDoc
        End Function
        Private Function FormatPostcode(Postcode As String) As String
            Dim FullURL As String = "http://www.192.com/places/"
            Do Until Postcode.Contains(" ") = False
                Postcode = Replace(Postcode, " ", "")
            Loop
            If Len(Postcode) > 7 Or Len(Postcode) < 5 Then
                Return ""
            End If
            If Len(Postcode) = 5 Then
                FullURL &= Mid(Postcode, 1, 1) & "/"
                FullURL &= Mid(Postcode, 1, 2) & "-" & Mid(Postcode, 3, 1) & "/"
                FullURL &= Mid(Postcode, 1, 2) & "-" & Mid(Postcode, 3) & "/"
            End If
            If Len(Postcode) = 6 Then
                If IsNumeric(Mid(Postcode, 2, 1)) Then
                    FullURL &= Mid(Postcode, 1, 1) & "/"
                    FullURL &= Mid(Postcode, 1, 3) & "-" & Mid(Postcode, 4, 1) & "/"
                    FullURL &= Mid(Postcode, 1, 3) & "-" & Mid(Postcode, 4) & "/"
                Else
                    FullURL &= Mid(Postcode, 1, 2) & "/"
                    FullURL &= Mid(Postcode, 1, 3) & "-" & Mid(Postcode, 4, 1) & "/"
                    FullURL &= Mid(Postcode, 1, 3) & "-" & Mid(Postcode, 4) & "/"
                End If
            End If
            If Len(Postcode) = 7 Then
                FullURL &= Mid(Postcode, 1, 2) & "/"
                FullURL &= Mid(Postcode, 1, 4) & "-" & Mid(Postcode, 5, 1) & "/"
                FullURL &= Mid(Postcode, 1, 4) & "-" & Mid(Postcode, 5) & "/"
            End If
            Return FullURL
        End Function
        Private Sub GetAddresses(ObjDoc As mshtml.HTMLDocument)
            Dim Obj As mshtml.IHTMLElementCollection = ObjDoc.getElementsByTagName("td")
            Addresses = New List(Of Address)
            For Each TD As mshtml.HTMLTableCell In Obj
                If TD.className = "address" Then
                    Dim FullAddress As String = TD.innerText
                    Addresses.Add(New Address(FullAddress))
                End If
            Next        
        End Sub
    End Class
    Public Class Address
        Property Line1 As String
        Property Line2 As String
        Property Line3 As String
        Property Line4 As String
        Property Postcode As String
        Public Sub New(FullAddress As String)
            Dim Obj As Object = Split(FullAddress, ", ")
            Select Case UBound(Obj)
                Case 4
                    Line1 = Obj(0) & " " & Obj(1)
                    Line2 = ""
                    Line3 = Obj(2)
                    Line4 = Obj(3)
                    Postcode = Obj(4)
                Case 5
                    Line1 = Obj(0) & " " & Obj(1)
                    Line2 = Obj(2)
                    Line3 = Obj(3)
                    Line4 = Obj(4)
                    Postcode = Obj(5)
                Case 6
                    Line1 = Obj(0) & " " & Obj(1)
                    Line2 = Obj(2) & " " & Obj(3)
                    Line3 = Obj(4)
                    Line4 = Obj(5)
                    Postcode = Obj(6)
            End Select
        End Sub
    End Class

对不起,如果代码有点乱,自学成才的程序员!

在NL中,您可以通过邮政编码和门牌号唯一地标识地址。

并非所有国家都有此属性,因此您的情况可能会有所不同。

但是你可以这样做:

SELECT 
  CONCAT(street,' ','$housenumber') AS streetplusnumber 
  , city
FROM postcodetostreet p
WHERE p.postcode = '$postcode' and '$housenumber' between minnumber and maxnumber

表的邮政编码到街道看起来像:

postcodetostreet
------------------
postcode varchar(6) primary key
street varchar(512)
city
minnumber
maxnumber

数据库通常是从第三方购买的。

我发现了这个(通过http://ben-major.co.uk/2012/02/using-google-maps-to-lookup-uk-postcodes/),同时寻找这个自己的答案。希望能有所帮助:

/*fill out the postcode and hit search*/
(function($) {
$.fn.searchPc = function(options) {
    var settings = $.extend({
        address1: 'address1',
        address2: 'address2',
        address3: 'address3',
        address4: 'address4'
    }, options);
    return this.each(function() {
        var $el = $(this);
        var $form = $el.closest('form');
        //insert the button on the form
        $('<a class="postCodeLookup">Search</a>').insertAfter($el);
        $('.postCodeLookup', $form).button({icons:{primary:'ui-icon-search'}});
        $form.on('click', '.postCodeLookup', function() {
            $.post('http://maps.googleapis.com/maps/api/geocode/json?address='+$el.val()+'&sensor=false', function(r) {
                var lat = r['results'][0]['geometry']['location']['lat'];
                var lng = r['results'][0]['geometry']['location']['lng'];
                $.post('http://maps.googleapis.com/maps/api/geocode/json?latlng='+lat+','+lng+'&sensor=false', function(address) {
                    $('input[name='+settings.address1+']').val(address['results'][0]['address_components'][0]['long_name']);
                    $('input[name='+settings.address2+']').val(address['results'][0]['address_components'][1]['long_name']);
                    $('input[name='+settings.address3+']').val(address['results'][0]['address_components'][2]['long_name']);
                    $('input[name='+settings.address4+']').val(address['results'][0]['address_components'][3]['long_name']);
                });
            });
        });

    });
};
})(jQuery);

    $('input[name=postcode]').searchPc({
        address2: 'custom_field',
    });
http://jsfiddle.net/rxFBj/3/

我知道你在英国,不幸的是,这意味着你要花一大笔钱才能获得皇家邮政的邮政编码地址文件(PAF)。

你可以使用像谷歌地理定位api这样的web服务。

您的意思是像下面这样的服务吗?

http://www.postcodeanywhere.co.uk/demos/address-finder.aspx

我只建议我们在工作中使用它,它没有给我们带来任何问题,但我想还有很多其他同样好的选择。

我相信这是一个付费服务。

如果你想在你的地址表单中添加邮政编码查找解决方案,你需要获得一个付费服务来访问皇家邮政的邮政编码地址文件。

Ideal Postcodes为英国提供了这样的解决方案。您可以在这里查看演示:https://ideal-postcodes.co.uk/postcode-lookup-demo