Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi ,
I'm trying to use the code brow but i'm always getting the error 'The remote server returned an error: (403) Forbidden.' on the line ' Dim response As HttpWebResponse = request.GetResponse()'.
I replaced [YOUR KEY GOES HERE] with my key (-> are the [ ] necessary?).

anyone can help?

VB
Private Sub Geocode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Geocode.Click
       txtLatLon.Text = GetLatLon(txtAddress.Text)
End Sub


VB
Public Function GetLatLon(ByVal addr As String) As String
        Dim url As String = "http://maps.google.com/maps/geo?output=csv&key=[YOUR KEY GOES HERE]&q=" & addr
        Dim request As System.Net.WebRequest = WebRequest.Create(url)
        Dim response As HttpWebResponse = request.GetResponse()
        If response.StatusCode = HttpStatusCode.OK Then
            Dim ms As New System.IO.MemoryStream()
            Dim responseStream As System.IO.Stream = response.GetResponseStream()
            Dim buffer(2048) As Byte
            Dim count As Integer = responseStream.Read(buffer, 0, buffer.Length)
            While count > 0
                ms.Write(buffer, 0, count)
                count = responseStream.Read(buffer, 0, buffer.Length)
            End While
            responseStream.Close()
            ms.Close()
            Dim responseBytes() As Byte = ms.ToArray()
            Dim encoding As New System.Text.ASCIIEncoding()
            Dim coords As String = encoding.GetString(responseBytes)
            Dim parts() As String = coords.Split(",")
            Return Convert.ToDouble(parts(2)) & Convert.ToDouble(parts(3))
        End If
        Return Nothing
    End Function
Posted
Updated 3-Aug-18 22:15pm
v3

found this (myself) to be working (don't forget to change the language setting 'language=nl' to the one you want ;) ):

VB
'see explanation on:
   'https://developers.google.com/maps/documentation/geocoding/index
   '
   Public Function GetLatLon(ByVal addr As String) As String
       Try
           Dim url As String = "http://maps.googleapis.com/maps/api/geocode/xml?address=" & addr & "&sensor=false&language=nl"
           Dim request As System.Net.WebRequest = WebRequest.Create(url)
           Dim response As HttpWebResponse = request.GetResponse()
           If response.StatusCode = HttpStatusCode.OK Then
               Dim ms As New System.IO.MemoryStream()
               Dim responseStream As System.IO.Stream = response.GetResponseStream()
               Dim buffer(2048) As Byte
               Dim count As Integer = responseStream.Read(buffer, 0, buffer.Length)
               While count > 0
                   ms.Write(buffer, 0, count)
                   count = responseStream.Read(buffer, 0, buffer.Length)
               End While
               responseStream.Close()
               ms.Close()
               Dim responseBytes() As Byte = ms.ToArray()
               Dim encoding As New System.Text.ASCIIEncoding()
               Dim Resp As String = encoding.GetString(responseBytes)
               Dim Pos As Integer = InStr(Resp, "<lat>") - 1
               Dim Nb As Integer = CharCount(Resp, "<lat>")
               'structure of response = x times  <lat>50.8469547</lat>   <lng>3.6013676</lng>
               Dim Coords(Nb, 2) As String
               Dim Counter As Integer = 1
               Do Until InStr(Pos, Resp, "<lat>", CompareMethod.Text) = 0
                   Coords(Counter, 1) = Mid(Resp, InStr(Pos, Resp, "<lat>", CompareMethod.Text) + 5, InStr(Pos, Resp, "</lat>", CompareMethod.Text) - (InStr(Pos, Resp, "<lat>", CompareMethod.Text) + 5))
                   Coords(Counter, 2) = Mid(Resp, InStr(Pos, Resp, "<lng>", CompareMethod.Text) + 5, InStr(Pos, Resp, "</lng>", CompareMethod.Text) - (InStr(Pos, Resp, "<lng>", CompareMethod.Text) + 5))
                   Pos = InStr(Pos, Resp, "</lng>", CompareMethod.Text) + 5
                   Counter = Counter + 1
               Loop
               Resp = ""
               For Counter = 1 To Nb
                   Resp = Resp & Coords(Counter, 1) & "," & Coords(Counter, 2) & ";"
               Next
               Return Resp
           Else
               MsgBox(response.StatusCode.ToString, MsgBoxStyle.Exclamation, "fout")
               Return ""
           End If
       Catch ex As Exception
           MsgBox(ex.ToString)
           Return ""
       End Try
   End Function
 
Share this answer
 
v2
what is the charcount fnction??
 
Share this answer
 
Comments
Richard Deeming 6-Aug-18 12:00pm    
If you want to ask a question about a solution, click the "Have a Question or Comment?" button under that solution.

DO NOT post your comment using the "Add your solution here" box!
Member 14513101 13-Sep-19 12:06pm    
That's what I'd like to know.
Richard Deeming 13-Sep-19 12:09pm    
So why are you asking me, instead of posting a comment on Solution 1?!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900