Questions Regarding Bing Maps Auto Suggest API Data Formatting

Balasaheb Molawade 136 Reputation points
2025-02-07T12:03:02.5433333+00:00

Hi, 

We are using the Bing Maps Auto Suggest API, available at the following link:

https://www.bing.com/api/maps/sdk/mapcontrol/isdk/autosuggestui#JS

 We are currently facing the following challenges and would appreciate your assistance: 

  1. State, County, and Country Abbreviations: We need to retrieve abbreviations for state, county, and country from the API. This is crucial for us, as we use these values to match records in our database. However, at times, the full state name returned by the API does not match our stored values, especially when working across different countries. Could you confirm if there is a way to obtain standardized abbreviations?
  2. Retrieving a Complete List of Names: Is it possible to obtain a complete list of state, county, and country names returned by the Auto Suggest API? This would help us in verifying and mapping data accurately.
  3. Address Suggestions for Italy: We noticed discrepancies in how Italian administrative divisions are structured. For example, the API suggests "Emilia-Romagna" as the state, but we store it as "Bologna – Metropolitan City" in our database, which aligns with how Italy defines its administrative divisions. Is there a way to retrieve more precise or regionally accurate data to better match our requirements?

We would appreciate any insights or solutions you can provide regarding these concerns. Please let us know if any configurations or alternative approaches can be used to achieve the desired results.

Looking forward to your response.

Thanks!

 

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

2 answers

Sort by: Most helpful
  1. rbrundritt 19,041 Reputation points Microsoft Employee
    2025-02-07T17:24:36.3+00:00

    In response to each numbered question:

    1. I can confirm that the autosuggest API will not return the state and country names in just one format. The data in this API comes from a lot of different data providers and has a mix of different data types (Place, local business, Address). In some cases standard codes are used (e.g. state abbreviation like WA for Washington) and others the full or alternate name. The one exception is that I believe this API returns a countryRegionIso2 property. I don't recall ever seeing differences in the county field. If you are focused on a limited set of countries, you could create a simple lookup table for these values and data map them to your preferred format. What you are looking to do is called address standardization (or normalization), which is not a feature provided by Bing or Azure Maps. There are specialized services for this available by 3rd parties, there are some open source solutions as well. Have you geocoded all the addresses in your database with Bing Maps? By doing this, and updating the address fields with what is returned would make it so that those addresses are aligned with what is currently in the Bing Maps system and this will then allow you to do the matching on the fly with good accuracy (may not match 100% of the time).
    2. No, this is not publicly exposed and can potentially change over time.
    3. No. Address data changes daily on a global scale. Formatting, places with multiple alternate names, geopolitical sensitive names... Text matching on address data in general has a ton of complications and will always have inaccuracies. There are several different ways to match address data. Text matching works if you sanitize both the data in your database and the input you get from the user (or Autosuggest API) to normalize it. Doing this for state and country names on a global scale using a lookup table is fairly easy. County names however are number more difficult as there tends to be a lot more discrepancies with these. Another method that is often used is to look at the coordinates and do a search for nearby records. Combine this with text matching on just the address line field after that, or simply show all records near that inputted location (what most systems do).
    1 person found this answer helpful.

  2. Manas Mohanty 465 Reputation points Microsoft Vendor
    2025-02-07T17:04:57.0366667+00:00

    Hi Balasaheb Molawade!

    Welcome to Azure Maps Q and A forum. Thank you for posting your query here.

    Here are suggested solution for requirement

    1.State, County, and Country Abbreviations:

    You can create a mapping dictionary in your application that translates full names returned by Api to abbreviations based on your database requirements

    Attached sample python code for explaining.

    # Sample mapping table
    mapping_table = {
        'California': 'CA',
        'New York': 'NY',
        'Emilia-Romagna': 'ER',
        'Lazio': 'LAZ',
        # Add more mappings as needed
    }
    # Function to get abbreviation
    def get_abbreviation(full_name):
        return mapping_table.get(full_name)  # Returns abreviation for fullname
    
    # Example usage with API response
    api_response = {
        'address': {
            'adminDistrict': 'California',
            'countryRegion': 'United States'
        }
    }
    state_full_name = api_response['address']['adminDistrict']
    country_full_name = api_response['address']['countryRegion']
    # Get abbreviations
    state_abbreviation = get_abbreviation(state_full_name)
    country_abbreviation = get_abbreviation(country_full_name)
    print(f"State: {state_abbreviation}, Country: {country_abbreviation}")
    

    Output

       CA 
    

    2.Retrieving a Complete List of Names

    You can parse the JSON response and use python SDK to filter out locality, state and countries. Attached sample python code for reference.

    import json
    # Sample JSON response
    json_data = '''{
        "authenticationResultCode": "ValidCredentials",
        "brandLogoUri": "http:\/\/dev.virtualearth.net\/Branding\/logo_powered_by.png",
        "copyright": "Copyright c 2018 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.",
        "resourceSets": [
            {
                "estimatedTotal": 1,
                "resources": [
                    {
                        "__type": "Autosuggest:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1",
                        "value": [
                            {
                                "__type": "LocalBusiness",
                                "address": {
                                    "countryRegion": "United States",
                                    "locality": "Kirkland",
                                    "adminDistrict": "WA",
                                    "countryRegionIso2": "US",
                                    "postalCode": "98034",
                                    "addressLine": "12412 116th Ave NE",
                                    "formattedAddress": "12412 116th Ave NE, Kirkland, WA 98034"
                                },
                                "name": "El Burrito Mojado"
                            },
                            {
                                "__type": "LocalBusiness",
                                "address": {
                                    "countryRegion": "United States",
                                    "locality": "Kirkland",
                                    "adminDistrict": "WA",
                                    "countryRegionIso2": "US",
                                    "postalCode": "98032",
                                    "addressLine": "111 116th Ave NE",
                                    "formattedAddress": "111 116th Ave NE, Kirkland, WA 98032"
                                },
                                "name": "El Burrito Mojado"
                            },
                            }
                        ]
                    }
                ]
            }
        ],
        "statusCode": 200,
        "statusDescription": "OK",
        "traceId": "b9a0e48d52164cdfa239f4152a7940e4|CO36161B45|7.7.0.0"
    }'''
    # Load the JSON data
    data = json.loads(json_data)
    # Extract city, state, and country names
    for resource_set in data['resourceSets']:
        for resource in resource_set['resources']:
            for value in resource['value']:
                address = value['address']
                country = address.get('countryRegion', 'N/A')
                city = address.get('locality', 'N/A')
                state = address.get('adminDistrict', 'N/A')
                print(f"City: {city}, State: {state}, Country: {country}")
    

    Output:

    City: Kirkland, State: WA, Country: United States
    City: Kirkland, State: WA, Country: United States
    
    
    

    3.Address Suggestions for Italy:

    You can again use map table approach to address discrepancy in Italy administrative divisions.

    Attached sample code for reference.

    
    import requests
    azure_maps_key = 'YOUR_AZURE_MAPS_KEY'
    query = 'Emilia-Romagna'
    url = f'https://atlas.microsoft.com/search/address/json?api-version=1.0&query={query}&subscription-key={azure_maps_key}'
    response = requests.get(url)
    data = response.json()
    # Extract relevant information
    for result in data['results']:
        address = result['address']['freeformAddress']
        admin_district = result['address']['adminDistrict']
        country = result['address']['country']
        print(f"Address: {address}, Admin District: {admin_district}, Country: {country}")
    # Create a mapping table based on your database structure
    mapping_table = {
        'Emilia-Romagna': 'Bologna – Metropolitan City',
        # Add other mappings as needed
    }
    # Check and map the admin district
    mapped_admin_district = mapping_table.get(admin_district)
    print(f"Mapped Admin District: {mapped_admin_district}")
    
    #Output
    
    Bologna – Metropolitan City
    

    Please don't forget to upvote this answer if it is helpful to you.

    Please let us know if you have additional queries.

    Thank you.


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.