Microsoft Azure Maps Geolocation Client Library for Python
Azure Maps Geolocation Package client library for Python
This package contains a Python SDK for Azure Maps Services for Geolocation. Read more about Azure Maps Services here
Azure SDK Python packages support for Python 2.7 has ended 01 January 2022. For more information and questions, please refer to https://github.com/Azure/azure-sdk-for-python/issues/20691
- Python 3.7 or later is required to use this package.
- An Azure subscription and an Azure Maps account.
- A deployed Maps Services resource. You can create the resource via Azure Portal or Azure CLI.
If you use Azure CLI, replace
<account-name> of your choice, and select a proper pricing tier based on your needs via the
<sku-name> parameter. Please refer to this page for more details.
az maps account create --resource-group <resource-group-name> --account-name <account-name> --sku <sku-name>
Install the package
Install the Azure Maps Service Geolocation SDK.
pip install azure-maps-geolocation
Create and Authenticate the MapsGeolocationClient
To create a client object to access the Azure Maps Geolocation API, you will need a credential object. Azure Maps Geolocation client also support two ways to authenticate.
1. Authenticate with a Subscription Key Credential
You can authenticate with your Azure Maps Subscription Key.
Once the Azure Maps Subscription Key is created, set the value of the key as environment variable:
Then pass an
AZURE_SUBSCRIPTION_KEY as the
credential parameter into an instance of AzureKeyCredential.
from azure.core.credentials import AzureKeyCredential from azure.maps.geolocation import MapsGeolocationClient credential = AzureKeyCredential(os.environ.get("AZURE_SUBSCRIPTION_KEY")) geolocation_client = MapsGeolocationClient( credential=credential, )
2. Authenticate with an Azure Active Directory credential
- Install azure-identity
- Register a new AAD application
- Grant access to Azure Maps by assigning the suitable role to your service principal. Please refer to the Manage authentication page.
Next, set the values of the client ID, tenant ID, and client secret of the AAD application as environment variables:
You will also need to specify the Azure Maps resource you intend to use by specifying the
clientId in the client options. The Azure Maps resource client id can be found in the Authentication sections in the Azure Maps resource. Please refer to the documentation on how to find it.
from azure.maps.geolocation import MapsGeolocationClient from azure.identity import DefaultAzureCredential credential = DefaultAzureCredential() geolocation_client = MapsGeolocationClient( client_id="<Azure Maps Client ID>", credential=credential )
The Azure Maps Geolocation client library for Python allows you to interact with each of the components through the use of a dedicated client object.
MapsGeolocationClient is the primary client for developers using the Azure Maps Geolocation client library for Python.
Once you initialized a
MapsGeolocationClient class, you can explore the methods on this client object to understand the different features of the Azure Maps Geolocation service that you can access.
Async clients and credentials should be closed when they're no longer needed. These
objects are async context managers and define async
The following sections provide several code snippets covering some of the most common Azure Maps Geolocation tasks, including:
This service will return the ISO country code for the provided IP address. Developers can use this information to block or alter certain content based on geographical locations where the application is being viewed from.
from azure.maps.geolocation import MapsGeolocationClient BLOCK_COUNTRY_LIST = ['US', 'TW', 'AF', 'AX', 'DL'] INCOME_IP_ADDRESS = "2001:4898:80e8:b::189" geolocation_result = client.get_country_code(ip_address=INCOME_IP_ADDRESS) result_country_code = geolocation_result.iso_code if result_country_code in BLOCK_COUNTRY_LIST: raise Exception("These IP address is from forebiden country")
Maps Geolocation clients raise exceptions defined in Azure Core.
This list can be used for reference to catch thrown exceptions. To get the specific error code of the exception, use the
error_code attribute, i.e,
This library uses the standard logging library for logging. Basic information about HTTP sessions (URLs, headers, etc.) is logged at INFO level.
Detailed DEBUG level logging, including request/response bodies and unredacted headers, can be enabled on a client with the
import sys import logging from azure.maps.geolocation import MapsGeolocationClient # Create a logger for the 'azure.maps.geolocation' SDK logger = logging.getLogger('azure.maps.geolocation') logger.setLevel(logging.DEBUG) # Configure a console output handler = logging.StreamHandler(stream=sys.stdout) logger.addHandler(handler)
Still running into issues? If you encounter any bugs or have suggestions, please file an issue in the Issues section of the project.
More sample code
Several Azure Maps Geolocation Python SDK samples are available to you in the SDK's GitHub repository. These samples provide example code for additional scenarios commonly encountered while working with Maps Geolocation
set AZURE_SUBSCRIPTION_KEY="<RealSubscriptionKey>" pip install azure-maps-geolocation --pre python samples/sample_authentication.py python sample/sample_get_country_code.py
--preflag can be optionally added, it is to include pre-release and development versions for
pip install. By default,
piponly finds stable versions.
Further detail please refer to Samples Introduction
For more extensive documentation on Azure Maps Geolocation, see the Azure Maps Geolocation documentation on docs.microsoft.com.
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
- Initial release
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Hashes for azure-maps-geolocation-1.0.0b1.zip
Hashes for azure_maps_geolocation-1.0.0b1-py3-none-any.whl