chrisje181
Gebruiker
- Lid geworden
- 23 okt 2008
- Berichten
- 106
input string is not in the right format.
Code:
Imports System.IO
Imports System.Net
Imports System.Web
Imports System.Collections.Generic
Imports System.ComponentModel
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
''' <summary>
''' Call the GetLatLon method to geocode a physical address
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub btnGeocode_Click(sender As System.Object, e As System.EventArgs) Handles btnGeocode.Click
'Dim link As String
'Dim link2 As String
'Dim adres As String
'adres = txtAddress.Text
'link = "https://maps.google.com/maps?q=" + adres
'link2 = "http://maps.google.com/maps/geo?q=" + adres + "&output=csv&oe=utf8&sensor=false"
''WEB CLIENT IS NEEDED TO DO THE DOWNLOAD
'Dim MyWebClient As New System.Net.WebClient
''BYTE ARRAY HOLDS THE DATA
'Dim ImageInBytes() As Byte = MyWebClient.DownloadData(link2)
''CREATE A MEMORY STREAM USING THE BYTES
'Dim ImageStream As New IO.MemoryStream(ImageInBytes)
''CREATE A BITMAP FROM THE MEMORY STREAM
'Form2.Show()
'Form2.WebBrowser1.Navigate(link2)
'Dim address As String
'address = txtAddress.Text
'System.Diagnostics.Process.Start("https://maps.google.com/maps?q=" + address)
Try
Dim ll As New LatLon()
Dim test
ll = GetLatLon(txtAddress.Text)
'txtLatLon.Text = test.ToString()
txtLatLon.Text = ll.Latitude.ToString() & ", " & ll.Longitude.ToString()
Catch ex As Exception
MessageBox.Show(ex.Message, "An error has occurred")
End Try
End Sub
''' <summary>
''' This function makes a call using the Google Maps API to geocode a physical
''' address and report the
''' latitude and longitude of the address
'''
''' You will need to obtain a Google Maps API key and plug it into the url string the
''' space indicated in order for this
''' code to execute properly
'''
''' The code could be useful to you if you need to geocode some addresses in order to
''' display them on a map, for example if
''' your site had a store locator, this code could be used to find the lat/lon of
''' each store. I would recommend obtaining the
''' addresses and keeping them in a database table rather than querying Google maps
''' for the lat/lon of the address each time it
''' is needed. Google does have limitations upon the number of free queries it will
''' support, at that, it might be useful to just
''' write an application to loop through all of the addresses, geocode each address,
''' and then write those captured lat/lon values
''' into your table.
'''
''' Whilst useful, there is nothing particulary interesting about this code; I think
''' it is pretty well covered in the Google Maps API documentation
''' </summary>
''' <param name="addr"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function GetLatLon(ByVal addr As String) As LatLon
Dim url As String = "http://maps.googleapis.com/maps/api/geocode/xml?sensor=false&address=" & 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 New LatLon(Convert.ToDouble(parts(2)), Convert.ToDouble(parts(3)))
End If
Return Nothing
End Function
End Class