Finding full address from postcode, what is the solution?

Matt Ginn picture Matt Ginn · Nov 1, 2011 · Viewed 12k times · Source

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.

Answer

Richard Harrison picture Richard Harrison · Aug 29, 2012

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!