Location autocomplete is a nice way of providing real-time location suggestions based on input text by the user. This little feature could be invaluable in a lot of applications.

If you want just a location autocomplete textbox, with no map control in your app, I suggest you look at Google Places API. It is astoundingly well documented and you could find your way around with minimal fuss(pun intended). The Google Places AutoComplete API will give you place suggestions based on text typed in search text box, but it does not return latitude-longitude coordinates of the suggested places. Lat-lon coordinate values are required to center the map control. That would require a call to another API, Geocoding API. This API call would require you to use Google Maps as mentioned in the API terms of use.

For all the uncertainties associated with using Google Maps on a WP device, the use of Bing Maps is recommended. The Bing equivalent to Google Places API is Bing Locations API. With the use of Bing Location Services you have to contend with the fact that it gives you far fewer data than the Google API. Having said that, I do expect Bing to improve their location mapping soon enough.

Right, having settled with the exclusive use of Bing Maps and its REST Services, you can go ahead and open up a new project. You can use a TextBox control, but the toolkit provides us with a very neat PhoneTextBox control. You would need to add the Microsoft.Phone.Controls.Toolkit reference to your project and also add the xmlns:toolkit=”clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit” to the xaml page. This control has a hint text which disappears when the textbox is in focus. You can also add an image within the textbox and can make it look something like this:

Moving along, drop a map control onto your design page. Set the Credential Provider property of the map control to the Bing Map Key. If you don’t have a Bing Map Key then you can sign in and register your app to obtain one here. Also drop a ListBox onto the page and set its Visibility property to ‘Collapsed’. This list box will hold the suggestions matching the text in the textbox.

You’re xaml should look something like this:

On textchanged event of the PhoneTextBox we could send the contents of the textbox to the Locations API url:https://dev.virtualearth.net/REST/v1/Locations?q=’InputTextSearch’&o=json&key=’YourMapAPIKey’&maxResults=10

Add the RestSharp and Newton.Json packages from Nuget to your project using the Package Manager Console. Add the following classes to your xaml.cs to read the json response of url above.

Read the json response in the textchanged event of the textbox and bind the model to the listbox data item. The model here is RequestData.

When the ListBox gets populated, you can select the desired location and display the selected location in the PhoneTextBox on the selectionchanged event of listbox.

The screenshots look like this:

Its fairly simple to work with location autocomplete using Bing Services. A single url REST call returns an object containing matching locations and all details associated with it including city, state, country, latitude and longitude which could be very useful information in applications.