0
votes

I'm using Bing Maps API on a WPF DevExpress Map to return information of the specific geolocation (longitude,latitude).

What I need to have is Address, Zip code, City and Country as separate items out of the query.

The problem is that Bing Maps (through DevExpress.Xpf.Map.GeocodeRequestResult) returns the address String in a non-unique format, depending on the location (I suspect country). For example the address String might be returned as:

  1. Address, City, Country, Zip (in Russia for example)
  2. Address, Zip City (in Germany)
  3. Address, City, Zip (in USA)

...and the list continues.

How can I get out Address, Zip code, City and Country out of Bing Maps query correctly independent of the form it is returned?

1

1 Answers

0
votes

I'm not too familiar with the maps in DevExpress but I do know that you can get a structured address object from Bing Maps. If you use the Bing Maps REST services you can reverse geocode a coordinate an have it return a structured address. Since you are working in WPF you can find documentation on how to use the Bing Maps REST services in .NET here: http://msdn.microsoft.com/en-us/library/jj819168.aspx

http://msdn.microsoft.com/en-us/library/jj870778.aspx

Here is an updated version of the GetResponse method:

private async Task<Response> GetResponse(Uri uri)
{
    System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
    var response = await client.GetAsync(uri);

    using (var stream = await response.Content.ReadAsStreamAsync())
    {
        DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Response));
        return ser.ReadObject(stream) as Response;
    }
}

Once you have the DataContracts in your project and a method to call the REST services, it is just a matter of putting together your REST request. Documentation on how to create a request for the reverse geocoder can be found here: http://msdn.microsoft.com/en-us/library/ff701710.aspx

Once you get a response from the service you will can get the address like this:

Response r = await GetResponse(new Uri("YOUR REST service request"));

if (r != null &&
    r.ResourceSets != null &&
    r.ResourceSets.Length > 0 &&
    r.ResourceSets[0].Resources != null &&
    r.ResourceSets[0].Resources.Length > 0)
{
    var location = r.ResourceSets[0].Resources[0] as BingMapsRESTService.Common.JSON.Location;

    var address = location.Address;
}