

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 Amazon Location MapLibre Geocoder GL 外掛程式
<a name="dev-maplibre-geocoder"></a>

Amazon Location MapLibre 地理編碼器外掛程式旨在讓您在使用 [maplibre-gl-geocoder](https://github.com/maplibre/maplibre-gl-geocoder) 程式庫進行映射轉譯和地理編碼時，更輕鬆地將 Amazon Location 功能整合到您的 JavaScript 應用程式。

## 安裝
<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 Location 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);
  ```