Resolved Winform Call to Google API to get full address

divjoy

Well-known member
Joined
Aug 25, 2013
Messages
159
Programming Experience
1-3
Hi,

Im designing a winform app to capture the full address using a ggole API to complete the adresses of CLients being added to the system automatically.

I have the details from Google A Geocoding API request takes the following form:


I have succeeded before using google API to get distance between post codes but am hitting a brick wall here. as the REQUEST_DENIED is all I get back, although I know my key is correct!

Here is my vb code to date, using Newtson.Json and http libraries

I have a simple Form with 3 textboes for the address and 1 button to execute the code
VB.NET:
Imports System.IO
Imports System.Net
Imports System.Net.Http
Imports Newtonsoft.Json
Imports System.Text.RegularExpressions

Public Class Form1
   
    Private API_Key As String = "********************************"
    Private rgx As New Regex("[^a-zA-Z0-9 ]")

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.tbEircode.Text = "****"
    End Sub

    Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim EDate As DateTime

        Dim address = "" ' "https://maps.googleapis.com/maps/api/distancematrix/json?origins=D07+KF59+ire&destinations=D07+KF59+ire&language=en-EN&key=" & Me.API_Key.ToString & "&sensor=false"
        ' Dim Eircodes(20) As String 'Array
        ' Dim x, y, z As Integer
        Try
                     
                '*****  Check Eircode First ******
                If Me.tbEircode.Text = "" Then
                    MessageBox.Show("No Eircode, please correct")
                    Exit Sub
                End If
             
                MessageBox.Show(Me.tbEircode.Text & " Eircode sent to Google Maps")
                '***************  Get them in order with | and destination address
                Dim OrgAddress As String = ""
                OrgAddress = Me.tbEircode.Text & ",IRELAND"
                MsgBox(OrgAddress)
               'alt test works OK, but not full address
               'address = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=D07+KF59+ire&destinations=F92+VE2H+ire&language=en-EN&key=" & Me.API_Key.ToString & "&sensor=false"
                address = "https://maps.googleapis.com/maps/api/geocode/json?components=postal_code:" & OrgAddress & "language=en-EN&key=" & Me.API_Key.ToString
                MsgBox(address)
                Using client As New Net.Http.HttpClient(), result = Await client.GetAsync(address)
                    If result.IsSuccessStatusCode Then
                        Dim content = Await result.Content.ReadAsStringAsync
                        Dim response = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Response)(content)
                        Me.tbAddress1.Text = response.status
                        Me.tbAddress2.Text = response.origin_addresses.First 'ToString
                        Me.tbAddress3.Text = response.destination_addresses.First
             
                    Else
                        Me.tbAddress1.Text = "error downloading"
                    End If
                End Using
               
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Google Distance Calculator")

        End Try




    End Sub



    Class GMatrix
        Public Property status As String
        Public Property elements As String
        Public Property rows() As Array
        'Public Property Punchline As String
    End Class

    Class ValuePair
        Public Property value As Integer
        Public Property text As String
    End Class

    Class Element
        Public Property status As String
        Public Property duration As ValuePair
        Public Property distance As ValuePair
    End Class

    Class Row
        Public Property elements As Element()  'Array
    End Class

    Class Response
        Public Property status As String
        Public Property origin_addresses As String() ' Array
        Public Property destination_addresses As String() ' Array
        Public Property rows As Row() 'Array
    End Class

End Class
 
Did you resolve this? Did you get an example complete? You were able to get the full address with the google api. Can you share?
 
Back
Top