Amazon Location MapLibre ジオコーダー GL プラグインを使用する
Amazon Location MapLibre ジオコーダープラグインは、maplibre-gl-geocoder
インストール
モジュールで使用する 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
この例では、AWSSDK for JavaScript V3
// 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
この例では、Amazon Location のクライアントを使用して、API による認証をリクエストしています。
注記
ここで示す例の一部では、Amazon Location GeoPlacesClient を使用しています。このクライアントは、AWS SDK for JavaScript V3
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());
関数
以下は、Amazon Location MapLibre ジオコーダープラグインで使用される関数です。
buildAmazonLocationMaplibreGeocoderこのクラスは、他のすべての呼び出しのエントリポイントとなる
AmazonLocationMaplibreGeocderのインスタンスを作成します。スタンドアロン
GeoPlacesClientAPI コールの使用 (クライアントはGeoPlacesClientのインスタンス):const amazonLocationMaplibreGeocoder = buildAmazonLocationMaplibreGeocoder(client, { enableAll: true });統合
LocationClientAPI コールの使用 (クライアントは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);