

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用 Amazon 位置 MapLibre 地理编码器 GL 插件
<a name="dev-maplibre-geocoder"></a>

Amazon Location MapLibre 地理编码器插件旨在让您在使用库进行地图渲染和地理编码时更轻松地将 Amazon Location 功能整合到 JavaScript 应用程序中。[maplibre-gl-geocoder](https://github.com/maplibre/maplibre-gl-geocoder)

## 安装
<a name="geocoder-installation"></a>

安装来自 NPM 的 Amazon Location MapLibre 地理编码器插件，以便与模块一起使用。键入以下命令：

```
npm install @aws/amazon-location-for-maplibre-gl-geocoder
```

您也可以使用以下脚本导入 HTML 和 CSS 文件，以便直接在浏览器中使用：

```
<script src="https://cdn.jsdelivr.net/npm/@aws/amazon-location-for-maplibre-gl-geocoder@2"></script>
<link
  href="https://cdn.jsdelivr.net/npm/@aws/amazon-location-for-maplibre-gl-geocoder@2/dist/amazon-location-for-mlg-styles.css"
  rel="stylesheet"
/>
```

## 与模块一起使用-独立 GeoPlaces SDK
<a name="geocoder-module"></a>

此示例使用[AWS 适用于 JavaScript V3 的 SDK](https://github.com/aws/aws-sdk-js-v3) 来获取 GeoPlacesClient 要提供给库的，并[AuthHelper](https://github.com/aws-geospatial/amazon-location-utilities-auth-helper-js)对库进行身份验证。 GeoPlacesClient它 APIs 为地理编码器启用了所有功能。

```
// Import MapLibre GL JS
import maplibregl from "maplibre-gl";
// Import from the AWS JavaScript SDK V3
import { GeoPlacesClient } from "@aws-sdk/client-geo-places";
// Import the utility functions
import { withAPIKey } from "@aws/amazon-location-utilities-auth-helper";
// Import the AmazonLocationMaplibreGeocoder
import {
  buildAmazonLocationMaplibreGeocoder,
  AmazonLocationMaplibreGeocoder,
} from "@aws/amazon-location-for-maplibre-gl-geocoder";

const apiKey = "<API Key>";
const mapName = "Standard";
const region = "<Region>"; // region containing Amazon Location API Key

// Create an authentication helper instance using an API key and region
const authHelper = await withAPIKey(apiKey, region);

const client = new GeoPlacesClient(authHelper.getClientConfig());

// Render the map
const map = new maplibregl.Map({
  container: "map",
  center: [-123.115898, 49.295868],
  zoom: 10,
  style: `https://maps.geo.${region}.amazonaws.com/v2/styles/${mapStyle}/descriptor?key=${apiKey}`,
});

// Gets an instance of the AmazonLocationMaplibreGeocoder Object.
const amazonLocationMaplibreGeocoder = buildAmazonLocationMaplibreGeocoder(client, { enableAll: true });

// Now we can add the Geocoder to the map.
map.addControl(amazonLocationMaplibreGeocoder.getPlacesGeocoder());
```

## 在浏览器中使用-独立 GeoPlaces SDK
<a name="geocoder-browser"></a>

此示例使用 Amazon Location 客户端发出通过 API 密钥进行身份验证的请求。

**注意**  
其中一些示例使用了 Amazon 地点 GeoPlacesClient。此客户端基于[AWS 适用于 JavaScript V3 的软件开发工具包](https://github.com/aws/aws-sdk-js-v3)，允许通过 HTML 文件中引用的脚本调用 Amazon Location。

在 HTML 文件中包含以下内容：

```
<!-- Import the Amazon Location For Maplibre Geocoder -->
<script src="https://cdn.jsdelivr.net/npm/@aws/amazon-location-for-maplibre-gl-geocoder@2"></script>
<link
  href="https://cdn.jsdelivr.net/npm/@aws/amazon-location-for-maplibre-gl-geocoder@2/dist/amazon-location-for-mlg-styles.css"
  rel="stylesheet"
/>
<!-- Import the Amazon GeoPlacesClient -->
<script src="https://cdn.jsdelivr.net/npm/@aws/amazon-location-client@1"></script>
```

在 JavaScript 文件中包含以下内容：

```
const apiKey = "<API Key>";
const mapStyle = "Standard";
const region = "<Region>"; // region containing Amazon Location API key

// Create an authentication helper instance using an API key and region
const authHelper = await amazonLocationClient.withAPIKey(apiKey, region);

const client = new amazonLocationClient.GeoPlacesClient(authHelper.getClientConfig());

// Render the map
const map = new maplibregl.Map({
  container: "map",
  center: [-123.115898, 49.295868],
  zoom: 10,
  style: `https://maps.geo.${region}.amazonaws.com/v2/styles/${mapStyle}/descriptor?key=${apiKey}`,
});

// Initialize the AmazonLocationMaplibreGeocoder object
const amazonLocationMaplibreGeocoderObject = amazonLocationMaplibreGeocoder.buildAmazonLocationMaplibreGeocoder(
  client,
  { enableAll: true },
);

// Use the AmazonLocationWithMaplibreGeocoder object to add a geocoder to the map.
map.addControl(amazonLocationMaplibreGeocoderObject.getPlacesGeocoder());
```

## 函数
<a name="geocoder-functions"></a>

下面列出了 Amazon Location MapLibre 地理编码器插件中使用的函数：
+ `buildAmazonLocationMaplibreGeocoder`

  该类创建 `AmazonLocationMaplibreGeocder` 的一个实例，此实例是其他所有调用的入口点。

  使用独立的 `GeoPlacesClient` API 调用（客户端是 `GeoPlacesClient` 的实例）：

  ```
  const amazonLocationMaplibreGeocoder = buildAmazonLocationMaplibreGeocoder(client, { enableAll: true });
  ```

  使用整合的 `LocationClient` API 调用（客户端是 `LocationClient` 的实例）：

  ```
  const amazonLocationMaplibreGeocoder = buildAmazonLocationMaplibreGeocoder(client, {
    enableAll: true,
    placesIndex: placeIndex,
  });
  ```
+ `getPlacesGeocoder`

  返回可以直接添加到地图的 ready-to-use IControl 对象。

  ```
  const geocoder = getPlacesGeocoder();
  
  // Initialize map see: <insert link to initializing a map instance here>
  let map = await initializeMap();
  
  // Add the geocoder to the map.
  map.addControl(geocoder);
  ```