Home > GIS > Find Near Places using Geonames Web Service

Find Near Places using Geonames Web Service

September 5th, 2009 admin Leave a comment Go to comments

Public Sub FindNearPlaces(ByVal lat As Decimal, ByVal lon As Decimal, ByVal radius As Integer, ByVal maxrows As Integer)

        Windows.Forms.Cursor.Current = Cursors.WaitCursor
        Dim xmlreader As XmlNodeReader = Nothing
        Dim request As HttpWebRequest
        Dim response As HttpWebResponse = Nothing
        Dim reader As StreamReader
        Dim address As Uri
        Dim data As StringBuilder
        Dim byteData() As Byte
        Dim postStream As Stream = Nothing
        Dim iNode As Integer

        address = New Uri("http://ws.geonames.org/findNearbyPlaceName?")

        ' Create the web request   
        request = DirectCast(WebRequest.Create(address), HttpWebRequest)

        ' Set type to POST   
        request.Method = "POST"
        request.ContentType = "application/x-www-form-urlencoded"

        ' Create the data we want to send   
        'appId = "YahooDemo"
        'context = "Italian sculptors and painters of the renaissance" _
        '& "favored the Virgin Mary for inspiration"
        'query = "madonna"

        data = New StringBuilder()
        data.Append("lat=" + HttpUtility.UrlEncode(lat))
        data.Append("&lng=" + HttpUtility.UrlEncode(lon))
        data.Append("&radius=" + HttpUtility.UrlEncode(radius))
        data.Append("&maxRows=" + HttpUtility.UrlEncode(maxrows))

        ' Create a byte array of the data we want to send   
        byteData = UTF8Encoding.UTF8.GetBytes(data.ToString())

        ' Set the content length in the request headers   
        request.ContentLength = byteData.Length

        ' Write data   
        Try
            postStream = request.GetRequestStream()
            postStream.Write(byteData, 0, byteData.Length)
        Finally
            If Not postStream Is Nothing Then postStream.Close()
        End Try

        Try
            ' Get response   
            response = DirectCast(request.GetResponse(), HttpWebResponse)

            ' Get the response stream into a reader   
            reader = New StreamReader(response.GetResponseStream())

            ' Console application output     
            'MsgBox(reader.ReadToEnd())

            Try
                Dim doc As New XmlDocument
                doc.LoadXml(reader.ReadToEnd())
                xmlreader = New XmlNodeReader(doc)
                xmlreader.MoveToContent()
                iNode = -1
                While xmlreader.Read()
                    Select Case xmlreader.NodeType
                        Case XmlNodeType.Element
                            If xmlreader.Name = "name" Then
                                Me.trPlaces.Nodes.Add(xmlreader.ReadInnerXml)
                                iNode += 1
                            End If
                            If xmlreader.Name = "lat" Then
                                Me.trPlaces.Nodes(iNode).Nodes.Add(xmlreader.ReadInnerXml)
                                Me.trPlaces.Nodes(iNode).Nodes(0).ImageIndex = 2
                            End If
                            If xmlreader.Name = "lng" Then
                                Me.trPlaces.Nodes(iNode).Nodes.Add(xmlreader.ReadInnerXml)
                                Me.trPlaces.Nodes(iNode).Nodes(1).ImageIndex = 2
                            End If
                            If xmlreader.Name = "countryCode" Then
                                Me.trPlaces.Nodes(iNode).Nodes.Add(xmlreader.ReadInnerXml)
                                Me.trPlaces.Nodes(iNode).Nodes(2).ImageIndex = 2
                            End If
                            If xmlreader.Name = "countryName" Then
                                Me.trPlaces.Nodes(iNode).Nodes.Add(xmlreader.ReadInnerXml)
                                Me.trPlaces.Nodes(iNode).Nodes(3).ImageIndex = 2
                            End If
                            If xmlreader.Name = "distance" Then
                                Me.trPlaces.Nodes(iNode).Nodes.Add(xmlreader.ReadInnerXml)
                                Me.trPlaces.Nodes(iNode).Nodes(4).ImageIndex = 2
                            End If
                    End Select
                End While
            Finally
                If Not (xmlreader Is Nothing) Then
                    xmlreader.Close()
                End If
            End Try

            reader.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            If Not response Is Nothing Then response.Close()
            Windows.Forms.Cursor.Current = Cursors.Default
        End Try

    End Sub
Categories: GIS Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.