Extracted this from a project I'm working on to include Google Maps in a custom DotNetNuke module. This function calls the
Google Maps geocoding service via HTTP directly and parses the Lat/Long out of the returned XML. The main reason I wanted to post this was to test
SyntaxHighlighter integrated with this blog.
Imports System.Net
Imports System.Xml
'http://code.google.com/apis/maps/documentation/reference.html
Function fGeoCode(ByVal tmpIn As String)
'would be better as a DNN Setting
Dim tmpKey As String = "abcdefghijklmnopqrstuvwxyz"
Dim tmpLatLong As String = "0,0"
Dim url As String = "http://maps.google.com/maps/geo?q=" & Replace(tmpIn, " ", "+") & "&output=xml&key=" & tmpKey
Dim request As HttpWebRequest = WebRequest.Create(url)
Dim response As HttpWebResponse = request.GetResponse
'If status = 200 (OK), then load it up
If response.StatusCode = HttpStatusCode.OK Then
Dim reader As New IO.StreamReader(response.GetResponseStream())
Dim doc As New XmlDocument()
doc.Load(reader)
'the status returned in the XML also needs to be 200
'this is different from an HTTP status code
'it tells if the geocoding was sucessful
If doc.ChildNodes.Item(1).ChildNodes.Item(0).ChildNodes.Item(1).ChildNodes.Item(0).InnerText = "200" Then
'go straight to the XML for the
tmpLatLong = doc.ChildNodes.Item(1).ChildNodes.Item(0).ChildNodes.Item(2).ChildNodes.Item(2).InnerText
End If
reader.Close()
reader = Nothing
Else
'status is not 200(OK), set LatLong to 0,0
tmpLatLong = "0,0"
End If
fGeoCode = tmpLatLong
End Function