10.15.08
Google Maps API Geocoding function for VB.NET
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

Comments

There are currently no comments, be the first to post one.

Post Comment

Name (required)

Email (required)

Website

Enter the code shown above:

chris busse works with nonprofit organizations to help them make the most of internet, database, and application technologies and is also available to speak to your organization or group on a variety of topics

email | linkedin | flickr | twitter

Loading
Loading Viddler Videos

    Powered By Viddler