This example requests title, description, and location information from the PhoneBook SourceType and displays the information in a Console Window. The request for location information and the Longitude property return code are featured in bold, below.
try
{
MSNSearchService s = new MSNSearchService();
SearchRequest searchRequest = new SearchRequest();
int arraySize = 1;
SourceRequest[] sr = new SourceRequest[arraySize];
sr[0] = new SourceRequest();
sr[0].Source = SourceType.PhoneBook;
sr[0].ResultFields = ResultFieldMask.Title | ResultFieldMask.Description | ResultFieldMask.Location;
searchRequest.Query = "coffee";
searchRequest.Requests = sr;
// Enter the Application ID, in double quotation marks, supplied by the
// Developer Provisioning System, as the value of the AppID on the SearchRequest.
searchRequest.AppID = "YOUR_APP_ID_GOES_HERE";
searchRequest.CultureInfo = "en-US";
// Set Location to the center of Seattle, Washington, USA and set the
// radius value to 25.0 Miles. You can modify the Location, using decimal values
// for latitude and longitude, to return addresses in other supported markets.
searchRequest.Location = new Location();
double latitude = 47.603828;
double longitude = -122.328567;
double radius = 25.0;
searchRequest.Location.Latitude = latitude;
searchRequest.Location.Longitude = longitude;
searchRequest.Location.Radius = radius;
SearchResponse searchResponse;
searchResponse = s.Search(searchRequest);
foreach (SourceResponse sourceResponse in searchResponse.Responses)
{
Result[] sourceResults = sourceResponse.Results;
if (searchResponse.Responses[0].Total > 0)
{
Console.WriteLine(sourceResponse.Source.ToString() + " - Total Results: " + sourceResponse.Total.ToString());
Console.WriteLine();
}
foreach (Result sourceResult in sourceResults)
{
if ((sourceResult.Title != null) && (sourceResult.Title != String.Empty))
Console.WriteLine("Title: " + sourceResult.Title);
if ((sourceResult.Description != null) && (sourceResult.Description != String.Empty))
Console.WriteLine("Description: " + sourceResult.Description);
if (sourceResult.Location != null)
{
Console.WriteLine("Latitude: " + sourceResult.Location.Latitude.ToString());
Console.WriteLine("Longitude: " + sourceResult.Location.Longitude.ToString());
// Radius always returns '5,' regardless of the value requested.
Console.WriteLine("Radius: " + sourceResult.Location.Radius.ToString());
}
Console.WriteLine("*****************************************************");
}
Console.WriteLine("Press any key to exit.");
Console.ReadLine();
}
}
catch (System.Web.Services.Protocols.SoapException fault)
{
Console.WriteLine(fault.Detail.InnerText.ToString());
Console.WriteLine("Press any key to exit.");
Console.ReadLine();
}
catch (System.Net.WebException webx)
{
Console.WriteLine(webx.ToString());
}
The following sample output shows results of this query. Note that, if you run the same query, your output is likely to be different due to the fact that you ran your query at a later time.
PhoneBook - Total Results: 11103
Title: Tully's Coffee
Description: (206) 682-6575 - 701 5th Ave, Seattle, WA
Latitude: 47.604359
Longitude: -122.329779
Radius: 5
*****************************************************
Title: Starbucks Coffee
Description: (206) 447-9934 - 701 5th Ave, Seattle, WA
Latitude: 47.604359
Longitude: -122.329779
Radius: 5
*****************************************************
Title: Walters Waffles
Description: (206) 622-8020 - 701 5th Ave Ste 3600, Seattle, WA
Latitude: 47.604359
Longitude: -122.329779
Radius: 5
*****************************************************
Title: Starbucks
Description: (206) 622-5789 - 700 5th Ave # 4, Seattle, WA
Latitude: 47.604402
Longitude: -122.329798
Radius: 5
*****************************************************
Title: 4th Avenue Caffe
Description: (206) 340-9035 - 500 4th Ave, Seattle, WA
Latitude: 47.602469
Longitude: -122.329599
Radius: 5
*****************************************************
Title: Starbucks
Description: (206) 447-9934 - 701 4th Ave, Seattle, WA
Latitude: 47.604007
Longitude: -122.330905
Radius: 5
*****************************************************
Title: M & M Assoc
Description: (206) 625-3253 - 800 5th Ave, Seattle, WA
Latitude: 47.60506
Longitude: -122.33046
Radius: 5
*****************************************************
Title: Pioneer Mart
Description: (206) 903-6324 - 609 3rd Ave, Seattle, WA
Latitude: 47.602865
Longitude: -122.331462
Radius: 5
*****************************************************
Title: All City Coffee
Description: (206) 652-8331 - 125 Prefontaine Pl S, Seattle, WA
Latitude: 47.60146
Longitude: -122.329689
Radius: 5
*****************************************************
Title: Pegasus Coffee Bar
Description: (206) 682-3113 - 711 3rd Ave # 331, Seattle, WA
Latitude: 47.603602
Longitude: -122.332252
Radius: 5
*****************************************************
Press any key to exit.