

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# Amazon Location MapLibre ジオコーダー 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>

モジュールで使用する Amazon Location MapLibre ジオコーダープラグインを NPM からインストールします。以下のコマンドを使用します:

```
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 SDK for JavaScript V3](https://github.com/aws/aws-sdk-js-v3) を使用して GeoPlacesClient を取得しライブラリに提供し、[AuthHelper](https://github.com/aws-geospatial/amazon-location-utilities-auth-helper-js) を使用して GeoPlacesClient を認証します。これにより、ジオコーダーのすべての API が有効になります。

```
// 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 SDK for 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`

  マップにすぐに追加できる状態で 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);
  ```