Microsoft Azure Maps Client Library for Python

2.1.0 · active · verified Thu Apr 09

The `azure-mgmt-maps` library provides Python client APIs for managing Azure Maps accounts and resources. It enables programmatic interaction with Azure Maps, allowing operations such as creating, updating, deleting, and listing Maps accounts, as well as managing account keys. The current stable version is 2.1.0, released in September 2023, and it follows the Azure SDK for Python's release cadence, with major versions indicating potential breaking changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to authenticate with Azure, list existing Maps accounts, and create or update an Azure Maps account using the `azure-mgmt-maps` library. It uses `DefaultAzureCredential` for authentication, which is suitable for local development and production environments. Ensure you have an Azure subscription ID and an existing resource group.

import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.maps import MapsManagementClient

# Set your Azure Subscription ID as an environment variable (AZURE_SUBSCRIPTION_ID)
subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID", "")
resource_group_name = "myMapsResourceGroup" # Replace with your resource group name
maps_account_name = "myUniqueMapsAccount" # Replace with your desired Maps account name
location = "eastus" # Replace with your desired Azure region

if not subscription_id:
    print("Please set the AZURE_SUBSCRIPTION_ID environment variable.")
else:
    # Authenticate using DefaultAzureCredential
    # This credential will attempt to authenticate via several mechanisms,
    # including environment variables, managed identity, and Azure CLI/PowerShell.
    credential = DefaultAzureCredential()

    # Create a MapsManagementClient
    maps_client = MapsManagementClient(credential, subscription_id)

    print(f"Listing existing Maps accounts in subscription '{subscription_id}'...")
    for account in maps_client.accounts.list_by_subscription():
        print(f"  - {account.name} (Location: {account.location}, SKU: {account.sku.name})")

    print(f"\nCreating or updating Maps account '{maps_account_name}' in resource group '{resource_group_name}'...")
    try:
        # Ensure the resource group exists before creating a Maps account.
        # This example assumes the resource group already exists.
        maps_account = maps_client.accounts.begin_create_or_update(
            resource_group_name=resource_group_name,
            account_name=maps_account_name,
            account={
                "location": location,
                "sku": {"name": "G2"}, # 'S1' (Gen1) or 'G2' (Gen2) are common SKUs
                "tags": {"environment": "development", "project": "maps-demo"}
            }
        ).result()
        print(f"Successfully created/updated Maps account: {maps_account.name} (SKU: {maps_account.sku.name})")

        print(f"\nGetting keys for Maps account '{maps_account_name}'...")
        keys = maps_client.accounts.list_keys(resource_group_name, maps_account_name)
        print(f"  - Primary Key: {keys.primary_key}")

        # Optional: Delete the Maps account
        # print(f"\nDeleting Maps account '{maps_account_name}'...")
        # maps_client.accounts.begin_delete(resource_group_name, maps_account_name).result()
        # print(f"Maps account '{maps_account_name}' deleted successfully.")

    except Exception as e:
        print(f"An error occurred: {e}")

view raw JSON →