65.9K
CodeProject 正在变化。 阅读更多。
Home

在 Visual Basic WinForms 应用程序中使用 Yahoo! Local Search Service

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.29/5 (3投票s)

2008年1月19日

CPOL

4分钟阅读

viewsIcon

27253

downloadIcon

213

一篇关于在 Visual Basic WinForms 应用程序中使用 Yahoo! Local Search Service 的文章。

引言

Yahoo! 本地搜索服务通过 Yahoo! 开发者网络免费提供给开发者。本文将演示一种方法,该方法可用于使用基于邮政编码的业务名称或产品搜索来返回商家位置列表。

该服务返回以下信息

  • 标题(商家名称)
  • Street Address
  • 城市
  • 状态
  • 电话号码
  • 纬度和经度
  • 与用户提供的邮政编码中心的距离
  • 商家地图和详细信息的 URL
  • 商家网站的 URL(如果可用)

本文将演示提交本地搜索服务请求、显示结果以及访问搜索结果中提供的链接的基础知识。当然,我们可以通过多种方式呈现搜索结果,此演示应用程序仅说明了一种可能性。有关该程序的更多信息,请直接参阅 Yahoo 开发者网络网站,网址为这里

该服务每天最多可以访问 50,000 次;参与该计划是免费的,前提是您注册了 Yahoo! 开发者网络并从 Yahoo! 获取了应用程序 ID。

在访问 Yahoo! 网站时,请查看可用的不同程序,并查看通过 Yahoo! 提供的许多服务。

图 1:正在运行的演示应用程序。

图 2:提供上下文菜单访问地图、商家和商家详细信息 URL。

图 3:显示商家的地图。

图 4:显示商家网站。

图 5:显示 Yahoo! 商家详细信息。

入门

要开始使用,请解压缩包含的项目并在 Visual Studio 2005 环境中打开解决方案。在解决方案资源管理器中,您应该注意这些文件

图 6:解决方案资源管理器。

主窗体 (frmSearch.vb)

主窗体 (frmSearch.vb) 是应用程序中包含的唯一窗体;访问服务、返回本地搜索结果以及在 datagridview 控件中或作为网页显示这些结果所需的所有应用程序特定代码都包含在此类中。

代码非常简单,如果您想在 IDE 中打开代码视图,您会看到代码文件如下所示

''' <summary>
''' Consume the Yahoo! Local Search Service
''' in a Visual Basic WinForms application
''' </summary>
''' <remarks></remarks>
Public Class frmSearch

    ''' <summary>
    ''' Default Constructor
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' the user's zip code likely won't change so
        ' set the zip code to user last entered zip code
        Me.txtZipCode.Text = My.MySettings.Default.MyZipCode

    End Sub

请注意,在构造函数中,邮政编码文本框控件填充了存储在应用程序中的用户设置。每次用户更改邮政编码时,上次输入的邮政编码都会存储在此设置中,下次使用该应用程序时,上次的邮政编码会恢复到文本框中。

下一段代码是搜索按钮的按钮单击事件处理程序。单击此按钮时,该方法将验证用户是否输入了搜索词和邮政编码。它会将当前的邮政编码存储到用户设置中(以防用户更改了它),然后它将调用一个名为 LocalSearch 的单独方法,该方法接受两个参数:商家或产品名称的标题,以及邮政编码。

''' <summary>
''' Initiate a Yahoo! Local Search Service
''' request based upon the user supplied
''' product/business name and zip code
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub btnSearch_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnSearch.Click

    ' validate that search terms were entered
    ' by making sure that the business/product
    ' and the zip code textboxes contain something
        If txtBusinessOrProduct.Text <> String.Empty Or _
            txtZipCode.Text <> String.Empty Then

            ' save last entered zip code in user setting
            ' so that the next time the application is used
            ' we can retrieve this zip code and reuse it
            My.MySettings.Default.MyZipCode = txtZipCode.Text
            My.MySettings.Default.Save()
    
            ' call the search with the product and zip code arguments
            ' this function will populate the datagrid with the
            ' values returned from the product search
            LocalSearch(txtBusinessOrProduct.Text, txtZipCode.Text)
    
        Else
            ' tell the user that the search requested is invalid
            ' if they failed to fill in both the product/business and
            ' zip code textboxes
            MessageBox.Show("You must supply both a zip code and
            product/business name.", "Search Invalid")
        End If

    End Sub

下一段代码用于执行本地搜索。代码的第一部分将商家或产品名称存储到字符串变量中,然后修剪邮政编码字符串以防止 zip+4 条目;接受邮政编码的文本框限制为只有五个字符的长度,因此这实际上不是必需的。然后格式化 HTTP Web 请求以针对 Yahoo! 本地搜索服务执行搜索;此请求使用提供的商家或产品名称以及提供的邮政编码来限制返回的结果数量。该服务将结果限制为最多 20 个条目。

派生的响应流用于填充数据集;然后数据集的第二个表绑定到 datagridview。第二个表包含搜索结果;该服务总共返回三个表,其中包含有关商家评级和搜索返回的总点击次数的信息。

''' <summary>
''' Conduct a search for products or business names
''' within a specified zip code using the Yahoo!
''' Local Search Service
''' </summary>
''' <remarks></remarks>
Private Sub LocalSearch(ByVal business As String, _
    ByVal zipCode As String)

    Try

        ' business or product name
        Dim biz As String = business

        ' take only the zip code (not zip + 4)
        Dim zip As String = zipCode.Substring(0, 5)

        ' format the Web request using
        ' the zip code and business/product name
        ' passed in as arguments
        ' NOTE:  The appid should be replaced with the
        ' appid supplied by Yahoo! when the developer
        ' signs up with the free developer network
        Dim request As System.Net.HttpWebRequest _
            = System.Net.WebRequest.Create( _
              "http://local.yahooapis.com/LocalSearchService/V2/_
              localSearch?appid=YahooDemo&query=" + biz + "&zip=" + zip + "&results=20")

        ' get HTTP Web response for the Web request
        Using response As System.Net.HttpWebResponse = _
            request.GetResponse()

            ' populate the dataset using the
            ' response stream (in XML)
            Dim dsSearch As DataSet = New DataSet()
            dsSearch.ReadXml(response.GetResponseStream())

            ' populate the datagridview control using the
            ' second table (which contains the actual search
            ' results - in all three tables are returned
            dataGridView1.DataSource = dsSearch.Tables(1)
            dataGridView1.Refresh()

        End Using

    Catch ex As Exception

        ' describe any encountered error if the
        ' local search should fail for any reason
        MessageBox.Show(ex.Message, "Error")

    End Try

End Sub

类中包含的最后三个方法用于打开任何特定业务的结果中提供的业务、地图和详细信息链接。

''' <summary>
''' Open the Map URL for the currently selected row
''' contained in the results datagridview control
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub mapURLToolStripMenuItem_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles mapURLToolStripMenuItem.Click

    Try
        ' get the cell value for the map URL
        Dim url As String
        url = dataGridView1.SelectedRows(0).Cells(10).Value.ToString()
    
        ' open the URL into a browser window
        System.Diagnostics.Process.Start(url)

    Catch
        ' display an error message to the user
        ' if the URL does not exist or was invalid
        MessageBox.Show("Invalid or Missing URL", "Map URL Error")
    End Try

End Sub

''' <summary>
''' Open the Business URL for the currently selected row
''' contained in the results datagridview control
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub businessURLToolStripMenuItem_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    businessURLToolStripMenuItem.Click

    Try
        ' get the cell value for the map URL
        Dim url As String
        url = dataGridView1.SelectedRows(0).Cells(11).Value.ToString()

        ' open the URL into a browser window
        System.Diagnostics.Process.Start(url)

    Catch
        ' display an error message to the user
        ' if the URL does not exist or was invalid
        MessageBox.Show("Invalid or Missing URL", "Business URL Error")
    End Try

End Sub

''' <summary>
''' Open the Business Details URL for the currently selected row
''' contained in the results datagridview control
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub detailsToolStripMenuItem_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles detailsToolStripMenuItem.Click

    Try
        ' get the cell value for the map URL
        Dim url As String
        url = dataGridView1.SelectedRows(0).Cells(8).Value.ToString()
    
        ' open the URL into a browser window
        System.Diagnostics.Process.Start(url)

    Catch
        ' display an error message to the user
        ' if the URL does not exist or was invalid
        MessageBox.Show("Invalid or Missing URL", "Business Details URL
        Error")
    End Try

End Sub

摘要

此应用程序作为如何在 Visual Basic WinForms 应用程序中利用 Yahoo! 本地搜索服务的一个示例提供。该服务返回有关商家的信息,并返回一个链接集合,该链接集合可用于显示商家的地图、商家网站以及 Yahoo! 收集的有关商家的信息。

© . All rights reserved.