Home > GIS > Find Near Places using Yahoo API

Find Near Places using Yahoo API

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

Public Sub FindNearPlaces(ByRef lat As String, ByRef lon As String, ByVal city As String)

        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://local.yahooapis.com/MapsService/V1/geocode?")

        ' 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("appid=Pf.jNBzV34HuXPFDcYWnBJcp_oUvDBUg7CtGrXT_0PFEWS4hp5bm.K.5k02AO5s-")
        data.Append("&city=" + city)

        ' 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 = "Latitude" Then
                                lat = xmlreader.ReadInnerXml()
                                iNode += 1
                            End If
                            If xmlreader.Name = "Longitude" Then
                                lon = xmlreader.ReadInnerXml()
                            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
            lat = ""
            lon = ""
        Finally
            If Not response Is Nothing Then response.Close()
            Windows.Forms.Cursor.Current = Cursors.Default
        End Try

    End Sub

#End Region
  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.