3

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.

Matt
  • 22,721
  • 17
  • 71
  • 112
Matt Ginn
  • 294
  • 1
  • 2
  • 10
  • ... and what exactly do you mean by "results of that postcode"? – Pekka Nov 01 '11 at 11:53
  • 2
    I'm sorry for being a bit too vague, I'm in the UK, I wanted the user to input postcode SW1 2LX for example and be returned with addresses in that postal code, eg, 1 some road, london – Matt Ginn Nov 01 '11 at 14:11

7 Answers7

2

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!

Richard Harrison
  • 355
  • 6
  • 19
  • 192 have changed their site. Add this before the getrequest: With request .AllowAutoRedirect = True .Timeout = 30000 .UserAgent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)" .PreAuthenticate = True .Credentials = CredentialCache.DefaultCredentials End With – Richard Harrison Feb 13 '13 at 00:10
1

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/

cfardella
  • 89
  • 8
  • 2
    Your application finds the Lat/Lng of the postcode and then does a reverse search of a single street address with that Lat/Lng, so it will only ever find one property. Not a solution to the original question. – Tom Dec 16 '15 at 12:09
1

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.

Johan
  • 74,508
  • 24
  • 191
  • 319
0

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

Dharman
  • 30,962
  • 25
  • 85
  • 135
Doaa-K
  • 1
  • 1
0

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).

Widor
  • 13,003
  • 7
  • 42
  • 64
0

You could use a web service like Googles geolocation api.

Martin Hoegh
  • 465
  • 3
  • 17
  • How? I don't think there is a way in the geo API to get all the possible addresses that match a postcode. – Pekka Nov 01 '11 at 11:54
  • Is that whats he wants? Input a zip code and list maybe list 5.000 full addresses? – Martin Hoegh Nov 01 '11 at 12:05
  • It looks like it. He's being very unclear. (Downvote is not mine, though, although even if it were correct, this would be a *very* thin answer. Maybe consider adding some links) – Pekka Nov 01 '11 at 12:11
0

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).

glosrob
  • 6,631
  • 4
  • 43
  • 73