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

发布于 2024-12-13 05:41:21 字数 99 浏览 1 评论 0原文

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

Basically I am making an application form for my site. I need to search for a full address using the user input postcode and would like to offer the results of that postcode for them to choose. I am aware that a database of sorts will be required but I am struggling to source this and would appreciate any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

自在安然 2024-12-20 05:41:21

在英国,您可以使用 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

如果代码有点乱,请自学程序员!

In the UK you can get a full address from any postcode using 192.com.

They have a completely free database available at 192.com

I do not work for them or any of their advertisers, but have used this site for many data entry applications.

Format the URL from the postcode you are searching. i.e.

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

This will then list all address in the postcode.

Here's the class I have written, hope it is of help:

    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

Sorry if the code is a little messy, self taught programmer!

2024-12-20 05:41:21

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

并非所有国家/地区都有此属性,因此您的里程可能会有所不同。

但是,您可以这样做:

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

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

In NL, you can uniquely identify an address by its postcode and the housenumber.

Not all countries have this property, so your mileage may vary.

However you'd do something like this:

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

The table postcode to street looks something like:

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

The database is usually purchased from a third party.

欢你一世 2024-12-20 05:41:21

我发现了这个(通过 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/

I found this (via http://ben-major.co.uk/2012/02/using-google-maps-to-lookup-uk-postcodes/) whilst searching for the answer to this myself. Hope it helps:

/*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/

雪落纷纷 2024-12-20 05:41:21

我看到您在英国,不幸的是,这意味着要花费大量金钱才能访问皇家邮政的 邮政编码地址文件 (PAF)。

I see you're in the UK, which unfortunately means forking out an obscene amount of money for access to the Royal Mail's Postcode Address File (PAF).

萌化 2024-12-20 05:41:21

您可以使用 Google 地理定位 api 等网络服务。

You could use a web service like Googles geolocation api.

谁对谁错谁最难过 2024-12-20 05:41:21

您是指类似以下的服务吗?

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

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

不过,这是一项付费服务​​(我相信)。

Do you mean a service like the following?

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

I only suggest this as we use this at work and it hasn't given us any problems but I would imagine there are plenty of other equally as good options available.

This is a paid service though (I believe).

提赋 2024-12-20 05:41:21

如果您想在地址表单中添加邮政编码查找解决方案,则需要购买付费服务才能访问皇家邮政的邮政编码地址文件。

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

If you want to add a postcode lookup solution to your address form, you'll need to procure a paid service to access Royal Mail's Postcode Address File.

Ideal Postcodes offers a solution like this for the UK. You can view a demo here: https://ideal-postcodes.co.uk/postcode-lookup-demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文