Introduction
Yahoo’s Local Search Service is provided free of charge to developers through the Yahoo! Developer’s Network. This article will demonstrate one approach that may be used to consume this service to return a list of business locations through the use of a zip code based search for a business name or product.
The service returns the following information:
- Title (the name of the business)
- Street Address
- City
- State
- Phone Number
- Latitude and Longitude
- The distance from the center of the zip code supplied by the user
- URLs for maps and details about the business
- URL of the business web site (if available)
This article will demonstrate the basics of submitting Local Search Service request, displaying the results, and accessing the available links returned in the search results. Naturally there are a number of ways in which we could present the search results and this demonstration application only illustrates one such possibility. For more information regarding the program, refer directly to the Yahoo Developer Network website located here.
The service may be accessed up to 50,000 times a day; participation in the program is free and is contingent upon signing up for the Yahoo! Developers Network and obtaining an application ID from Yahoo!
When visiting the Yahoo! site, take a look at the different programs available and take a look at some of the many services made available through Yahoo.
Figure 1: The demonstration application running.
Figure 2: Providing Context Menu Access to the Map, Business, and Business Details URLs.
Figure 3: Displaying the Map to the Business.
Figure 4: Displaying the Business Website.
Figure 5: Displaying the Yahoo! Business Details.
Getting Started
In order to get started, unzip the included project and open the solution in the Visual Studio 2005 environment. In the solution explorer, you should note these files:
Figure 6: Solution Explorer.
The Main Form (frmSearch.vb)
The main form (frmSearch.vb) is the only form contained in the application; all of the application specific code required to access the service, return the results of an local search, and to display those results in a datagridview control or as a web page are included in this class.
The code is pretty simple, if you'd care to open the code view up in the IDE, you will see that the code file begins as follows:
Public Class frmSearch
Public Sub New()
InitializeComponent()
Me.txtZipCode.Text = My.MySettings.Default.MyZipCode
End Sub
Note that in the constructor, the zip code textbox control is populated from a user setting stored in the application. Each time the user changes the zip code, the last entered zip code is stored in this setting and the next time the application is used, the last zip code is restored into the textbox.
The next section of code is the button click event handler for the search button. When this button is clicked, the method will verify that the user has entered a search term and zip code. It will store the current zip code into the user setting (in case the user changed it) and then it will call a separate method called LocalSearch
which accepts two arguments: the title of the business or product name, and the zip code.
Private Sub btnSearch_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnSearch.Click
If txtBusinessOrProduct.Text <> String.Empty Or _
txtZipCode.Text <> String.Empty Then
My.MySettings.Default.MyZipCode = txtZipCode.Text
My.MySettings.Default.Save()
LocalSearch(txtBusinessOrProduct.Text, txtZipCode.Text)
Else
MessageBox.Show("You must supply both a zip code and
product/business name.", "Search Invalid")
End If
End Sub
The next section of the code is used to execute the local search. The first part of the code stores the business or product name into a string variable and then trims the zip code string to prevent a zip+4 entry; the textbox that accepts the zip code is limited to only five characters in length, so this is not really necessary. An HTTP Web request is then formatted to execute the search against the Yahoo! Local Search service; this request uses the supplied business or product name along with the supplied zip code to limit the number of results returned. The service limits the results to a maximum of 20 entries.
The derived response stream is used to populate a dataset; the dataset’s second table is then bound to the datagridview. The second table contains the results of the search; a total of three tables are returned by the service and contain information regarding the business rating and the total number of hits returned by the search.
Private Sub LocalSearch(ByVal business As String, _
ByVal zipCode As String)
Try
Dim biz As String = business
Dim zip As String = zipCode.Substring(0, 5)
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")
Using response As System.Net.HttpWebResponse = _
request.GetResponse()
Dim dsSearch As DataSet = New DataSet()
dsSearch.ReadXml(response.GetResponseStream())
dataGridView1.DataSource = dsSearch.Tables(1)
dataGridView1.Refresh()
End Using
Catch ex As Exception
MessageBox.Show(ex.Message, "Error")
End Try
End Sub
The final three methods contained in the class are used to open up the business, map, and details links provided in the results obtained for any specific business.
Private Sub mapURLToolStripMenuItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mapURLToolStripMenuItem.Click
Try
Dim url As String
url = dataGridView1.SelectedRows(0).Cells(10).Value.ToString()
System.Diagnostics.Process.Start(url)
Catch
MessageBox.Show("Invalid or Missing URL", "Map URL Error")
End Try
End Sub
Private Sub businessURLToolStripMenuItem_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
businessURLToolStripMenuItem.Click
Try
Dim url As String
url = dataGridView1.SelectedRows(0).Cells(11).Value.ToString()
System.Diagnostics.Process.Start(url)
Catch
MessageBox.Show("Invalid or Missing URL", "Business URL Error")
End Try
End Sub
Private Sub detailsToolStripMenuItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles detailsToolStripMenuItem.Click
Try
Dim url As String
url = dataGridView1.SelectedRows(0).Cells(8).Value.ToString()
System.Diagnostics.Process.Start(url)
Catch
MessageBox.Show("Invalid or Missing URL", "Business Details URL
Error")
End Try
End Sub
Summary
This application was provided as an example of how one might take advantage of the Yahoo! Local Search service in a Visual Basic WinForms application. The service returns information about the business and returns a collection of links that may be used to display a map of the business, the business Web site, and Yahoo! collected information pertaining to the business.