Get ISO 3166-2 Code from Bing Maps Locations API

1.3k views Asked by At

Is there a way to get the ISO 3166-2 Code for a specific PostalCode/CountryCode Combination with the Bing Maps Locations API?

Per Example: 1210/AT => AT-9

1

There are 1 answers

2
rbrundritt On

Use the Bing Maps REST Location API and geocode your query (postal, country...). As part of the request add &incl=ciso2 which will return the country's ISO2 code value. For example:

https://dev.virtualearth.net/REST/v1/Locations?q=1210%2C%20AT&incl=ciso2&key=YOUR_Bing_Maps_Key

Will return the following:

{
"authenticationResultCode": "ValidCredentials",
"brandLogoUri": "http:\/\/dev.virtualearth.net\/Branding\/logo_powered_by.png",
"copyright": "Copyright © 2017 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": "Location:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1",
        "bbox": [48.237850189209, 16.3536491394043, 48.3225288391113, 16.4731197357178],
        "name": "1210, Austria",
        "point": {
            "type": "Point",
            "coordinates": [48.2801895141602, 16.4104042053223]
        },
        "address": {
            "adminDistrict": "Vienna",
            "adminDistrict2": "Vienna",
            "countryRegion": "Austria",
            "formattedAddress": "1210, Austria",
            "locality": "Vienna",
            "postalCode": "1210",
            "countryRegionIso2": "AT"
        },
        "confidence": "High",
        "entityType": "Postcode1",
        "geocodePoints": [{
            "type": "Point",
            "coordinates": [48.2801895141602, 16.4104042053223],
            "calculationMethod": "Rooftop",
            "usageTypes": ["Display"]
        }],
        "matchCodes": ["Good"]
    }]
}],
"statusCode": 200,
"statusDescription": "OK",
"traceId": "cb93431f2caa4f9fb2fcdbaa0ae80b74|CO30324109|7.7.0.0|"
}

Notice in the address property of the response there is "countryRegionIso2": "AT"

Documentation on the Bing Maps REST Locaiton API can be found here: https://msdn.microsoft.com/en-US/library/ff701711.aspx

There is a best practices guide here: https://msdn.microsoft.com/en-us/library/dn894107.aspx

If using .NET you may want to use the Bing Maps REST .NET Toolkit: https://github.com/Microsoft/BingMapsRESTToolkit It makes it really easy to use the REST services in .NET and also has a NuGet package for easily including in your project.