@azure/maps-search

中文文档
1.0.0-beta.2发布于 2022/11/08MITTypeScriptESM + CJS近 30 天下载 4579

A client library for Azure Maps maps-search

此软件包已被标记为弃用

Please use @azure-rest/maps-search

原始使用文档
文档目录17 节
  1. Getting started
  2. Currently supported environments
  3. Prerequisites
  4. Install the @azure/maps-search package
  5. Create and authenticate a MapsSearchClient
  6. Key concepts
  7. MapsSearchClient
  8. Examples
  9. Request latitude and longitude coordinates for an address
  10. Search for an address or Point of Interest
  11. Make a Reverse Address Search to translate coordinate locat…
  12. Translate coordinate location into a human understandable c…
  13. Troubleshooting
  14. Logging
  15. Next steps
  16. Contributing
  17. Related projects

Azure Maps Search client library for JavaScript/TypeScript

The Azure Maps Search Service is a set of RESTful APIs designed to help developers search addresses, places, and business listings by name, category, and other geographic information. In addition to supporting traditional geocoding, services can also reverse geocode addresses and cross streets based on latitudes and longitudes. Latitude and longitude values returned by the search can be used as parameters in other Azure Maps services, such as Route and Weather services.

This package contains an isomorphic SDK (runs both in Node.js and in browsers) for Azure Maps Search client.

Source code | Package (NPM) | Samples | Product Information

Getting started

Currently supported environments

Prerequisites

If you use Azure CLI, replace <resource-group-name> and <map-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> --name <map-account-name> --sku <sku-name>

Install the @azure/maps-search package

Install the Azure Maps Search client library with npm:

npm install @azure/maps-search

Create and authenticate a MapsSearchClient

To create a client object to access the Azure Maps Search APIs, you will need a credential object. The Azure Maps Search client can use an Azure Active Directory credential or an Azure Key credential to authenticate.

Using an Azure Active Directory Credential

You can authenticate with Azure Active Directory using the Azure Identity library. To use the DefaultAzureCredential provider shown below, or other credential providers provided with the Azure SDK, please install the @azure/identity package:

npm install @azure/identity

You will also need to register a new AAD application and grant access to Azure Maps by assigning the suitable role to your service principal. Please refer to the Manage authentication page.

Set the values of the client ID, tenant ID, and client secret of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET.

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.

const { MapsSearchClient } = require("@azure/maps-search");
const { DefaultAzureCredential } = require("@azure/identity");

const credential = new DefaultAzureCredential();
const client = new MapsSearchClient(credential, "<maps-account-client-id>");

Using a Subscription Key Credential

You can authenticate with your Azure Maps Subscription Key.

const { MapsSearchClient, AzureKeyCredential } = require("@azure/maps-search");

const credential = new AzureKeyCredential("<subscription-key>");
const client = new MapsSearchClient(credential);

Key concepts

MapsSearchClient

MapsSearchClient is the primary interface for developers using the Azure Maps Search client library. Explore the methods on this client object to understand the different features of the Azure Maps Search service that you can access.

Examples

The following sections provide several code snippets covering some of the most common Azure Maps Search tasks, including:

Request latitude and longitude coordinates for an address

You can use an authenticated client to convert an address into latitude and longitude coordinates. This process is also called geocoding. In addition to returning the coordinates, the response will also return detailed address properties such as street, postal code, municipality, and country/region information.

const searchResult = await client.searchAddress("400 Broad, Seattle");
for (const {
  position: { lat, lon },
} of searchResult.results) {
  console.log(`Latitude: ${lat}, Longitude: ${lon}`);
}

Search for an address or Point of Interest

You can use Fuzzy Search to search an address or a point of interest (POI). The following example demonstrates how to search for pizza over the scope of a specific country (France, in this example).

const fuzzySearchResult = await client.fuzzySearch({ query: "pizza", countryCodeFilter: ["fr"] });
for (const result of fuzzySearchResult.results) {
  console.log(result);
}

Make a Reverse Address Search to translate coordinate location to street address

You can translate coordinates into human readable street addresses. This process is also called reverse geocoding. This is often used for applications that consume GPS feeds and want to discover addresses at specific coordinate points.

const coordinates: LatLon = [47.59118, -122.3327]; // [latitude, longitude]

const { addresses } = await client.reverseSearchAddress(coordinates);
for (const { address } of addresses) {
  console.log(address);
}

Translate coordinate location into a human understandable cross street

Translate coordinate location into a human understandable cross street by using Search Address Reverse Cross Street API. Most often, this is needed in tracking applications that receive a GPS feed from a device or asset, and wish to know where the coordinate is located.

const coordinates: LatLon = [47.59118, -122.3327];

const { addresses } = await client.reverseSearchCrossStreetAddress(coordinates);
for (const { address } of addresses) {
  console.log(address);
}

Troubleshooting

Logging

Enabling logging may help uncover useful information about failures. In order to see a log of HTTP requests and responses, set the AZURE_LOG_LEVEL environment variable to info. Alternatively, logging can be enabled at runtime by calling setLogLevel in the @azure/logger:

const { setLogLevel } = require("@azure/logger");
setLogLevel("info");

For more detailed instructions on how to enable logs, you can look at the @azure/logger package docs.

Next steps

Please take a look at the samples directory for detailed examples on how to use this library.

Contributing

If you'd like to contribute to this library, please read the contributing guide to learn more about how to build and test the code.

Impressions

版本与发布记录

@azure/maps-search 共发布 5 个版本,首次发布于 2022/10/01;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.0-beta.2latest2022/11/083 年前
1.0.0-beta.12022/10/144 年前
1.0.0-alpha.20221007.2dev2022/10/084 年前
1.0.0-alpha.20221003.32022/10/044 年前
1.0.0-alpha.20220930.22022/10/014 年前

关于 @azure/maps-search 的常见问题

@azure/maps-search 是什么?

A client library for Azure Maps maps-search注意:该包已被作者标记为弃用——Please use @azure-rest/maps-search

如何安装 @azure/maps-search?

在项目目录运行 npm install @azure/maps-search,或使用 pnpm add / yarn add / bun add。

@azure/maps-search 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@azure/maps-search 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@azure/maps-search 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=14.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@azure/maps-search 可以商用吗?

@azure/maps-search 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@azure/maps-search 的下载量有多少?

根据 npm 官方下载统计,@azure/maps-search 在 2026/10/08 – 2026/11/06 这 30 天内被下载 4,579 次,日均约 153 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@azure/maps-search 还在维护吗?

最新版本 1.0.0-beta.2 发布于 2022/11/08(3 年前);至今共发布 5 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/10/01。作者已将其标记为弃用,新项目建议寻找替代方案。