Azure maps fails to recognize country of address

Shimmy Weitzhandler 246 Reputation points
2025-03-02T07:36:01.9+00:00

Hi

Originally reported here, Azure maps fails to recognize the correct country of a query address.

This issue divides in two parts:

  1. The client fails to find the address in the specified country.
  2. The client does not allow limiting the search to a specific country/region.

Expected behavior

Address should be the specified place which is in the UK, the following was tried:

  • Prospect Place, Fairfax Drive, Westcliff-on-Sea
  • Prospect Place, Fairfax Drive, Westcliff-on-Sea, UK
  • Prospect Place, Fairfax Drive, Westcliff-on-Sea, United Kingdom

This occurs to other addresses too, here's another example: "Cleveland Drive, Westcliff-on-Sea".

Actual behavior

Address is: Prospect Ave, West Palm Beach, FL 33404, United States

Reproduction Steps


var address = "Prospect Place, Fairfax Drive, Westcliff-on-Sea, UK";
var client = new MapsSearchClient(credential);
var response = await client.GetGeocodingAsync(query: address, cancellationToken: cancellationToken);

var isSuccess = response.GetRawResponse().Status == StatusCodes.Status200OK;

if (!isSuccess || response.Value.Features.Count == 0)
{
    logger.LogWarning("Could not parse address {Address}", address);
    throw new InvalidOperationException($"Could not parse address '{address}'.");
}

var feature = response.Value.Features.First();
var result = feature.Properties.Address;

  • If I try to specify GeocodingQuery.CountryRegion in the query, the result is "Bad request: When 'query' parameter is present, only the following parameters are valid: 'bbox, coordinates, view, top'. 'countryRegion' parameter was passed".
  • Looking at the source code I can see that the Query property of the options parameter is ignored altogether.

Environment

VS 17.13.2

.NET 9.0.2

Azure Maps
Azure Maps
An Azure service that provides geospatial APIs to add maps, spatial analytics, and mobility solutions to apps.
795 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. rbrundritt 19,501 Reputation points Microsoft Employee
    2025-03-03T18:20:35.6833333+00:00

    Looking into this is appears "Prospect place" is the name of the building and not the name of the street the building is on. This is likely the main cause of the issue. The street address for this building is "10 Fairfax Dr, Westcliff-on-Sea". When using the building name the query becomes more of a point of interest search and you might have better luck using a different Azure Maps search service.

    As for why you are getting US results, the REST client libraries are currently in preview and were released a few years ago. I believe the search client library is still based on version 1 of the Azure Maps search service powered directly from TomTom which tends to favor US results and has OK address parsing and understanding capabilities. This service sometimes requires tuning the query options to get more accurate results. In this case you can limit the search to a specific country by setting the countrySet option to GB. Doing this results in a much better result: Fairfax Drive, Westcliff-on-Sea, SS0

    The Azure Maps geocoding service is the next iteration of the search service and is powered by the same data and intelligence as Bing Maps. It is much better at address parsing and understanding, and more globally aligned. Testing all variations of the address you provided with the geocoding service all return the same address:

    AddressLatitudeLongitudeFairfax Drive, Westcliff-on-Sea, SS051.5506070.68812This is much more accurate than what you were getting originally and the same result as v1 search service when you limit the search to the UK but without any option tuning needed. The main missing piece here is the street number since the search/geocoding service is only designed for street addresses, it doesn't understand the building name. The geocoding service isn't yet wrapped by a client library, so you would need to call this REST service directly and parse the response. However, keep reading.

    Since you have the building name, we can instead use the point of interest search service and we end up getting the exact result you were looking for:

    POI NameAddressLatitudeLongitudeProspects Place10 Fairfax Drive, Southend-on-Sea, SS0 9AG51.5503230.702416It does not look like this service is in the REST client library, so you would need to call this REST service directly and parse the results. Here is what the REST query URL would look like:

    https://atlas.microsoft.com/search/poi/json?api-version=1.0&query=Prospect%20Place%2C%20Fairfax%20Drive%2C%20Westcliff-on-Sea&view=Auto&subscription-key=

    Here is a simple example of how to do this in C#:

    string yourAzureMapsKey = "<Insert your Azure Maps Key here>";
    string query = "Prospect Place, Fairfax Drive, Westcliff-on-Sea";
    
    var httpClient = new HttpClient();
    
    //Create the request to the Azure Maps Point of Interest. Escape the query as a data string to ensure special characters do not cause issues.
    var request = new HttpRequestMessage(HttpMethod.Get, $"https://atlas.microsoft.com/search/poi/json?api-version=1.0&query={Uri.EscapeDataString(query)}&view=Auto&subscription-key={yourAzureMapsKey}");
    var response = await httpClient.SendAsync(request);
    
    if (response.StatusCode == System.Net.HttpStatusCode.OK)
    {
        try
        {
            var responseBody = await response.Content.ReadAsStringAsync();
    
            //Parse the response and extract the address information.
            var json = JsonDocument.Parse(responseBody);
            var results = json.RootElement.GetProperty("results");
    
            //Check to see if there is any results.
            if (results.GetArrayLength() > 0)
            {
                //Loop over the results and output the POI name, address, latitude, longitude as a markdown table.
                Console.WriteLine("| POI Name | Address | Latitude | Longitude |\n| -------- | ------- | -------- | --------- |");
    
                foreach(var r in results.EnumerateArray())
                {
                    var name = r.GetProperty("poi").GetProperty("name").GetString();
                    var address = r.GetProperty("address").GetProperty("freeformAddress").GetString();
                    var position = r.GetProperty("position");
                    var latitude = position.GetProperty("lat").GetDouble();
                    var longitude = position.GetProperty("lon").GetDouble();
    
                    Console.WriteLine($"| {name} | {address} | {latitude} | {longitude} |");
                }
            } 
            else
            {
                Console.WriteLine("No results found.");
            }
        } 
        catch (Exception e)
        {
            Console.WriteLine("Error parsing response: " + e.Message);
        }
    }
    else
    {
        Console.WriteLine("Error making request. Status code: " + response.StatusCode);
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.