你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用 Azure Maps TypeScript REST SDK

Azure Maps 为 Azure TypeScript REST SDK 提供了 npm 模块的集合。 这些模块由客户端库组成,通过 JavaScript 或 TypeScript,可以轻松地在 Web 或 Node.js 应用程序中使用 Azure Maps REST 服务。 有关可用模块的完整列表,请参阅 JavaScript/TypeScript REST SDK 开发人员指南

在 Web 应用程序中使用 REST SDK

  1. @azure-rest/maps-search 为例,使用 npm install @azure-rest/maps-search 安装包。

  2. 创建并验证 MapsSearch 客户端。 若要创建客户端以访问 Azure Maps 搜索 API,需要一个凭据对象。 客户端支持 Microsoft Entra 凭据Azure 密钥凭据 进行身份验证。 可能需要为不同的身份验证方法安装 @azure/identity@azure/core-auth

    如果您使用订阅密钥进行身份验证,请通过 npm install @azure/core-auth 安装软件包。

    import MapsSearch from "@azure-rest/maps-search";
    import { AzureKeyCredential } from "@azure/core-auth";
    
    // Get an Azure Maps key at https://azure.com/maps.
    const subscriptionKey = "<Your Azure Maps Key>";
    
    // Use AzureKeyCredential with a subscription key.
    const credential = new AzureKeyCredential(subscriptionKey);
    
    // Use the credential to create a client
    const client = MapsSearch(credential);
    

    如果使用 Microsoft Entra ID 进行身份验证,请使用 npm install @azure/identity 安装包:

    import MapsSearch from "@azure-rest/maps-search";
    import { InteractiveBrowserCredential } from "@azure/identity";
    
    // Enter your Azure AD client and tenant ID.
    const clientId = "<Your Azure Active Directory Client ID>";
    const tenantId = "<Your Azure Active Directory Tenant ID>";
    
    // Enter your Azure Maps client ID.
    const mapsClientId = "<Your Azure Maps Client ID>";
    
    // Use InteractiveBrowserCredential with Azure AD client and tenant ID.
    const credential = new InteractiveBrowserCredential({
      clientId,
      tenantId
    });
    
    // Use the credential to create a client
    const client = MapsSearch(credential, mapsClientId);
    

    有关详细信息,请参阅 使用 Azure Maps 进行身份验证

  3. 以下代码使用新创建的 Azure Maps 搜索客户端对地址进行地理编码:“1 Microsoft Way, Redmond, WA”。 该代码发出 GET 请求,并将结果显示为页面正文中的表。

    // Search for "1 microsoft way, redmond, wa".
    const html = [];
    const response = await client
      .path("/search/address/{format}", "json")
      .get({ queryParameters: { query: "1 microsoft way, redmond, wa" } });
    
    // Display the total results.
    html.push("Total results: ", response.body.summary.numResults, "<br/><br/>");
    
    // Create a table of the results.
    html.push("<table><tr><td>Result</td><td>Latitude</td><td>Longitude</td></tr>");
    response.body.results.forEach((result) => {
      html.push(
        "<tr><td>",
        result.address.freeformAddress,
        "</td><td>",
        result.position.lat,
        "</td><td>",
        result.position.lon,
        "</td></tr>"
      );
    });
    
    html.push("</table>");
    
    // Add the resulting HTML to the body of the page.
    document.body.innerHTML = html.join("");
    

下图显示了此示例代码的结果、搜索地址的表以及生成的坐标的屏幕截图。

HTML 表的屏幕截图,其中显示了搜索的地址和生成的坐标。

Azure 政府版云支持

Azure Maps Web SDK 支持 Azure 政府云。 用于访问 Azure Maps Web SDK 的所有 JavaScript 和 CSS URL 保持不变,但需要执行以下任务才能连接到 Azure Maps 平台的 Azure 政府版云版本。

在使用交互式地图控件时,请在创建 Map 类的实例之前添加以下代码行。

atlas.setDomain('atlas.azure.us');

在对地图和服务进行身份验证时,请务必使用 Azure 政府云平台中的 Azure Maps 身份验证详细信息。

使用 TypeScript REST SDK 时,需要在创建客户端实例时设置服务的域。 例如,以下代码创建 MapsSearch 类的实例,并将域指向 Azure 政府云。

const client = MapsSearch(credential, { baseUrl: 'https://atlas.azure.us'});

如果直接访问 Azure Maps REST 服务,请将 URL 域更改为 atlas.azure.us。 例如,如果使用搜索 API 服务,请将 URL 域从 https://atlas.microsoft.com/search/ 更改为 https://atlas.azure.us/search/

后续步骤

详细了解本文中使用的类和方法:

有关将 TypeScript REST SDK 与 Web SDK 集成配合使用的更多代码示例,请参阅以下文章: