Tuesday, March 20, 2007

This, is so cool, I just have to spread the word.    So I've been playing around with location based applications in the past few posts.     I thought (as you do),  wouldn't it be great to be able to continually find things of interest around your current location,  i.e Pizza restaurants, coffee shops that kind of stuff.

Seeing the superb job, of how Windows Live Search Mobile I wondered how they achieved this functionality, and further more could it be 'mashed' into a Line of business application.   Of course the answer is YES...

Windows Live Search is exposed as a set of web-services,  how cool..   Even better still it is also possible to search by proximity.  So a little digging on MSDN, I found out how to search for things near me.    First stop,  MSDN article, to setup the web-service and talks you through the basics:

http://msdn2.microsoft.com/en-us/library/bb251794.aspx

From this guide, weaned the following.     I firstly added a web-reference to a Windows Mobile 6 Project.  The web-reference URL is

http://soap.search.msn.com/webservices.asmx?wsdl

Then, I created an application ID,  by going to the following -

http://search.msn.com/developer

(steps lifted from getting started guide)

  • Click Create and Manage Application IDs.
  • Sign in with your Windows Live ID. If you do not have a Live ID, click Sign Up Now to create one. Live ID authentication is required to use the Developer Provisioning System.
  • Once you're signed in, click Get a new App ID to create a new Application ID for your application.
  • Type a friendly name for your application in the Application Name text box.
  • Read the Terms of Use and click I Accept to agree to these terms. If you do not agree to the Terms of Use, click I Do Not Accept. The Application ID that appears on the page will not be activated if you choose not to accept the Terms of Use.
  • Once you've accepted the Terms of Use, select and copy the Application ID and paste it into your code as the value for the AppID field
  •  

    Ok, so once done,  I then wrote the following method,  which will lookup from a given longitude and latitude of where you are a description of your choice, i.e Pizza's.

    Again this C# code is proof of concept adapted from one of the MSDN examples.   It returns everything as a big string, but gives you an idea for how the API works.

     

    public string SearchIt(string searchfor,double latitude,double longitude)
    {
    StringBuilder sbret=new StringBuilder();

    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].SortBy = SortByType.Distance;

    searchRequest.Query = searchfor;
    searchRequest.Requests = sr;
    searchRequest.AppID = "<YOUR ID, YOU WILL NEED TO GET ONE OF YOUR OWN, SEE GETTING STARTED GUIDE";
    searchRequest.CultureInfo = "en-US"
    Location l = new Location();
    l.Latitude = latitude;
    l.Longitude = longitude;
    l.Radius = 1000;

    searchRequest.Location = l;
    SearchResponse searchResponse;

    searchResponse = s.Search(searchRequest);

    foreach (SourceResponse sourceResponse in searchResponse.Responses)
    {
    Result[] sourceResults = sourceResponse.Results;
    if (sourceResponse.Total > 0)
    {
    sbret.AppendLine(sourceResponse.Source.ToString() + " - Total Results: " + sourceResponse.Total.ToString());
    sbret.AppendLine();
    }
    foreach (Result sourceResult in sourceResults)
    {
    if ((sourceResult.Title != null) && (sourceResult.Title != String.Empty))
    sbret.AppendLine("Title: " + sourceResult.Title);
    if ((sourceResult.Description != null) && (sourceResult.Description != String.Empty))
    sbret.AppendLine("Description: " + sourceResult.Description);
    if ((sourceResult.Url != null) && (sourceResult.Url != String.Empty))
    sbret.AppendLine("URL: " + sourceResult.Url);

    if (sourceResult.Location != null)
    sbret.AppendLine("Location: " + sourceResult.Location.Latitude + "," + sourceResult.Location.Longitude + " radius" + sourceResult.Location.Radius + ",");
    sbret.AppendLine("*****************************************************");
    }
    }
    }
    catch (SoapException fault)
    {
    sbret.AppendLine(fault.Detail.InnerText.ToString());
    }
    catch (WebException webx)
    {
    sbret.AppendLine(webx.ToString());
    }
    /* This is the end of the try...catch block. */
    return sbret.ToString();
    }

     

    And that my friends, is pretty much it.    The data is restricted to US and UK content at the moment,  but it should give you a flavor of what's possible.

    I was amazed as to how easy this was to get going,   let me know if I've missed some big step, or something but it works like magic for me.

    Theme design by Jelle Druyts

    Pick a theme: