Search code examples
c#wpfdictionarybing-mapsbing-api

Finding suggested addresses from a key word in bing search service


I have created a WPF application to find locations from the keyword entered. For that I have used bing maps service api my code is

    private object SearchKeywordLocation(string keywordLocation)
    {
        String results = "";
        SearchRequest searchRequest = new SearchRequest();

        // Set the credentials using a valid Bing Maps key
        searchRequest.Credentials = new SearchService.Credentials();
        searchRequest.Credentials.ApplicationId = "my key";

        //Create the search query
        StructuredSearchQuery ssQuery = new StructuredSearchQuery();
        string[] parts = keywordLocation.Split(';');
        ssQuery.Keyword = parts[0];
        ssQuery.Location = parts[1];
        searchRequest.StructuredQuery = ssQuery;

        //Define options on the search
        searchRequest.SearchOptions = new SearchOptions();
        searchRequest.SearchOptions.Filters =
            new FilterExpression()
            {
                PropertyId = 3,
                CompareOperator = CompareOperator.GreaterThanOrEquals,
                FilterValue = 8.16
            };

        //Make the search request
        SearchServiceClient searchService = new SearchServiceClient("BasicHttpBinding_ISearchService");
        SearchResponse searchResponse = searchService.Search(searchRequest);

        //Parse and format results
        if (searchResponse.ResultSets[0].Results.Length > 0)
        {
            StringBuilder resultList = new StringBuilder("");
            for (int i = 0; i < searchResponse.ResultSets[0].Results.Length; i++)
            {
                resultList.Append(String.Format("{0}. {1}\n", i + 1,
                    searchResponse.ResultSets[0].Results[i].Name));
            }

            results = resultList.ToString();
        }
        else
            results = "No results found";

        return results;
    }
}

for this app. I am getting results when I am calling SearchKeywordLocation("sushi; Arvada, CO"); but my requirement is to get results when I call SearchKeywordLocation("new"); I should get results related to new york. this specific string formatting should be avoided. What I am doing wrong here?


Solution

  • The Search service is for points of interests and not addresses. New York falls into the category of an address and is something that should be passed through the geocoding service. That said, passing in "New" into any of the services would unlike to get the result you want as there are millions of possible results that have the word "new" in their name. Given this the geocoder will likely identify that this is a poor formed query and limit the possible results to only a few (testing "new" I see 5 results, New York was not one of them).

    That said, you should also avoid using the old legacy SOAP services. They are nearing end of life and the documentation was taken offline a few years ago. In fact, we stopped recommending the SOAP services about 5 years ago and only kept them around for customers who have old apps running on them. These services were replaced with the Bing Maps services 5 years ago which have a lot more features and functionalities, are much faster, have smaller response objects and tend to return more accurate results. You can find documentation on using the REST services here: https://msdn.microsoft.com/en-us/library/ff701713.aspx

    Here is some documentation on using them in .NET: https://msdn.microsoft.com/en-us/library/jj819168.aspxI

    I've also been working on creating a nice .NET library that wraps the services to make them easier to use in these types of apps. If you are interested in testing it out, send me an email at ricky_brundritt at Hotmail.com and I'll send you over a copy of the library.