I have had a few people recently ask me how to reverse geocode a location to an address on Windows Phone 7. The answer should be straightfoward but at the moment thats not quite true.
So whats the straightforward answer supposed to be?
Well glad you asked
It should be to use the CivicAddressResolver class and its ResolveAddressAsync method
Ok so whats the Gotcha?
When you try to resolve an address CivicAddressResolver all you get back is a empty CivicAddress. Yep the resolve method is not implemented.
So what options do you have?
- Wait for the CivicAddressResolver to be implemented. Note: At the time of writing I have not heard of when it will be released.
- Use the Bing Maps API to perform your reverse geocode in one of the following ways
- Add a service reference from your WP7 project to the Bing Maps SOAP API <– Requires Bing Maps Key to be supplied in code so not ideal as we want to protect this.
- Call the Bing Maps REST API from within your WP7 project <– Requires Bing Maps Key to be supplied in code so not ideal as we want to protect this.
- Either of the above two options but access your Bing Maps Key on a service and request it over SSL for device use – ensures XAP does not contain key but still returning it to the device over the wire
- Proxy the call to the Bing Maps REST API in your own WCF Service so your Bing Maps Key never hits the device <– ideal method until CivicAddressResolver is implemented.
If you would prefer grab the code then reading through here it is – You can download the code from this linked page
The remainder of this post will detail how to go about implementing the last option above i.e Proxy the call to your own service so that the Key is not available on the WP7 device.
Creating the WCF Service
- In Visual Studio 2010 File –> Add New Project –> Select WCF Service Application
- Name the Project “ReverseGeocodeService”
- On ReverseGeocodeService Right Click –> Add Reference –> Select System.Configuration (we will use this for retrieving your Bing Maps key from configuration).
- Set the project to use the local IIS webserver as the host. On ReverGeocodeService Right Click –> Properties –> Web –> select Use Local IIS Web server and provide a project URL of http://localhost/ReverseGeocodeService
- Create the following Folder structure and add the following files within your service

- Create your BingLocationResponse class as follows:
Note this class details the how to serialize and deserialize a response from the Bing Maps Rest API and we will use it deserialize the JSON response from the Bing Maps REST API and then also use it both to return a response to the WP7 client
using System.Runtime.Serialization;
namespace ReverseGeocodeService.Contracts.Data
{
[DataContract]
public class BingLocationResponse
{
[DataMember]
public string authenticationResultCode { get; set; }
[DataMember]
public string brandLogoUri { get; set; }
[DataMember]
public string copyright { get; set; }
[DataMember]
public ResourceSet[] resourceSets { get; set; }
[DataMember]
public string statusCode { get; set; }
[DataMember]
public string statusDescription { get; set; }
[DataMember]
public string traceId { get; set; }
[DataContract]
public class ResourceSet
{
[DataMember]
public int estimatedTotal { get; set; }
[DataMember]
public Resource[] resources { get; set; }
[DataContract(Namespace = "http://schemas.microsoft.com/search/local/ws/rest/v1", Name = "Location")]
public class Resource
{
[DataMember]
public string __type { get; set; }
[DataMember]
public double[] bbox { get; set; }
[DataMember]
public string name { get; set; }
[DataMember]
public Point point { get; set; }
[DataContract]
public class Point
{
[DataMember]
public string type { get; set; }
[DataMember]
public string[] coordinates { get; set; }
}
[DataMember]
public Address address { get; set; }
[DataContract]
public class Address
{
[DataMember]
public string addressLine { get; set; }
[DataMember]
public string adminDistrict { get; set; }
[DataMember]
public string adminDistrict2 { get; set; }
[DataMember]
public string countryRegion { get; set; }
[DataMember]
public string formattedAddress { get; set; }
[DataMember]
public string locality { get; set; }
[DataMember]
public string postalCode { get; set; }
}
[DataMember]
public string confidence { get; set; }
[DataMember]
public string entityType { get; set; }
}
}
}
}
- Next Define the Interface for your WCF service IGeocodeService.cs and your Geocode Provider that will call out to Bing Maps
IGeocodeService.cs:
using System.ServiceModel;
using ReverseGeocodeService.Contracts.Data;
namespace ReverseGeocodeService.Contracts
{
[ServiceContract]
public interface IGeocodeService
{
[OperationContract]
string GetAddress(double latitude, double longitude);
[OperationContract]
BingLocationResponse GetFullAddress(double latitude, double longitude);
}
}
IGeocodeProvider.cs:
using System;
using ReverseGeocodeService.Contracts.Data;
namespace ReverseGeocodeService.Contracts
{
public interface IGeocodeProvider
{
BingLocationResponse ReverseGeocode(double latitude, double longitude);
}
}
- Implement your GeocodeProvider.cs
This class will call out to the Bing Maps REST API return its deserialized its response. If you want to see what other options Bing Maps REST API provides you should use this as your starting point and more specifically for the Locations by Point we are using in this example see this
using System;
using System.Diagnostics;
using System.Net;
using System.Runtime.Serialization.Json;
using ReverseGeocodeService.Contracts;
using ReverseGeocodeService.Contracts.Data;
namespace ReverseGeocodeService.Providers
{
public class GeocodeProvider : IGeocodeProvider
{
private readonly string _bingMapsKey;
private string _bingMapsRESTUri = "https://dev.virtualearth.net/REST/v1/Locations/{0}?key={1}";
public GeocodeProvider(string bingMapsKey)
{
_bingMapsKey = bingMapsKey;
}
public BingLocationResponse ReverseGeocode(double latitude, double longitude)
{
BingLocationResponse result = null;
string formattedLocation = string.Format("{0},{1}", latitude, longitude); //bing maps requires Lat,Long
var request = HttpWebRequest.Create(string.Format(_bingMapsRESTUri, formattedLocation, _bingMapsKey)) as HttpWebRequest;
try
{
using (var response = request.GetResponse() as HttpWebResponse)
{
result = GetResult(response);
}
}
catch (Exception ex)
{
Trace.WriteLine(ex.ToString());
}
return result;
}
private BingLocationResponse GetResult(HttpWebResponse response)
{
BingLocationResponse location = null;
if (response != null && response.StatusCode == HttpStatusCode.OK)
{
//Deserialize the response and provide the address to the callback action
using (var stream = response.GetResponseStream())
{
DataContractJsonSerializer serialiser = new DataContractJsonSerializer(typeof(BingLocationResponse));
location = serialiser.ReadObject(stream) as BingLocationResponse;
}
}
return location;
}
}
}
- Finally implement your WCF Service GecodeService.svc.cs as follows
Note that the BingKey is externalised into your config files appSettings, for the example to work you will need to supply your key to the config file.
using System.Configuration;
using System.Linq;
using System.ServiceModel;
using ReverseGeocodeService.Contracts;
using ReverseGeocodeService.Contracts.Data;
using ReverseGeocodeService.Providers;
namespace ReverseGeocodeService
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class GeocodeService : IGeocodeService
{
//TODO: set your bingkey in config appsetting named BingKey
private static IGeocodeProvider _geocodeProvider = new GeocodeProvider(ConfigurationManager.AppSettings["BingKey"]);
public string GetAddress(double latitude, double longitude)
{
string formattedAddress = null;
var geocodeResult = _geocodeProvider.ReverseGeocode(latitude, longitude);
if (geocodeResult != null
&& geocodeResult.resourceSets != null
&& geocodeResult.resourceSets.Any()
&& geocodeResult.resourceSets.First().resources != null
&& geocodeResult.resourceSets.First().resources.Any())
{
formattedAddress = geocodeResult.resourceSets.First().resources.First().address.formattedAddress;
}
return formattedAddress;
}
public BingLocationResponse GetFullAddress(double latitude, double longitude)
{
return _geocodeProvider.ReverseGeocode(latitude, longitude);
}
}
}
- Add the appSettings section to your Web.config and supply your Bing Maps API Key
<configuration>
<appSettings>
<add key="BingKey" value="Bing Maps Key Here"/>
</appSettings>
...
</configuration>
Note: you should encrypt this Key using whatever standard practices you use.
Creating the Windows Phone 7 Client
Note: this is not an example of how to create an MVVM implementation on WP7 i.e we are focusing on how to reverse geocode a location to an address through the use of a proxy service to protect your Bing Maps Key as such in this post the client code is kept straightforward to demonstrate usage.
- Add a Windows Phone 7 project File –> New Project –> Silverlight for Windows Phone –> Windows Phone Application
- Name the project ReverseGeocode
- On the project Right Click –> Add Service Reference –> Press Discover.
- You should see a window as in the following image. Provide the Namespace ReverseGeocodeClient and Press OK

Note: If this step is generating an Empty ServiceReferences.ClientConfig then delete the Service References and Service References.ClientConfig. Close all instances of Visual Studio. Open Visual Studio and try to add it again – it worked for me :). If this still doesn’t work then you will need to use SlSvcUtil.exe located in your Program Files (Program Files (x86)) for 64bit machines as follows:
C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v7.0\Tools\SlSvcUtil.exe” http://localhost/ReverseGeocodeService/GeocodeService.svc?wsdl
Take the two files from the output and include them in your project and continue.
- Right click on the generated ServiceReferences.ClientConfig and set its Build Action to Content and Copy to Output Directory to Copy if newer
- Add a couple of buttons to perform calls out to the service in your MainPage.xaml and a TextBlock to display the result
<phone:PhoneApplicationPage
x:Class="ReverseGeocode.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True" Loaded="PhoneApplicationPage_Loaded">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Click="btnGetAddress_Click" Content="Get Address" Height="72" HorizontalAlignment="Left" Margin="6,6,0,0" Name="button1" VerticalAlignment="Top" Width="270" />
<TextBlock x:Name="txtAddress" Height="203" HorizontalAlignment="Left" Margin="12,163,0,0" Text="Address goes here" VerticalAlignment="Top" Width="425" />
<Button Content="Get Full Response" Height="72" HorizontalAlignment="Left" Margin="5,72,0,0" Name="button2" VerticalAlignment="Top" Width="270" Click="btnGetFullResponse_Click" />
</Grid>
</Grid>
</phone:PhoneApplicationPage>
- Update your codebehind, MainPage.xaml.cs to call out to your new Reverse geocoding service when buttons are clicked
using System;
using System.Linq;
using System.Windows;
using Microsoft.Phone.Controls;
using ReverseGeocode.ReverseGeocodeClient;
namespace ReverseGeocode
{
public partial class MainPage : PhoneApplicationPage
{
private GeocodeServiceClient _client;
private const string NO_RESULT_FOUND = "No Result Found";
// Constructor
public MainPage()
{
InitializeComponent();
}
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
_client = new GeocodeServiceClient();
_client.GetAddressCompleted += new EventHandler(client_GetAddressCompleted);
_client.GetFullAddressCompleted += new EventHandler(_client_GetFullAddressCompleted);
}
private void btnGetAddress_Click(object sender, RoutedEventArgs e)
{
_client.GetAddressAsync(-33.796526, 151.138267);
}
private void btnGetFullResponse_Click(object sender, RoutedEventArgs e)
{
_client.GetFullAddressAsync(-33.796526, 151.138267);
}
void client_GetAddressCompleted(object sender, GetAddressCompletedEventArgs e)
{
txtAddress.Text = e.Result ?? NO_RESULT_FOUND;
}
void _client_GetFullAddressCompleted(object sender, GetFullAddressCompletedEventArgs e)
{
string result = NO_RESULT_FOUND;
if (e.Result != null
&& e.Result.resourceSets != null
&& e.Result.resourceSets.Any()
&& e.Result.resourceSets.First().resources != null
&& e.Result.resourceSets.First().resources.Any())
{
var x = e.Result.resourceSets.First().resources.First();
result = string.Format ("Address:\n{0}\nConfidence:\n{1}\n", x.address.formattedAddress, x.confidence);
}
txtAddress.Text = result;
}
}
}
Note: I have not done it in this example however when you are finished with your _client you should always call _client.CloseAsync();

Summary:
And there you have it – in the absence of CivicAddressResolver this provides an implementation to reverse geocode a latitude and longitude into an Address on Windows Phone 7 without the need for your Bing Maps key to either be stored in the XAP or retrieved from a service by keeping your key within the context of your WCF proxy service.
You can download the code from this linked page – note you will have to add your Bing Maps Key to the web.config for it to work and also create the virtual directory on local host as detailed in the WCF service steps.
I hope this has helped you save some time 
Enjoy,
Nick