Cookie の設定を選択する

当社は、当社のサイトおよびサービスを提供するために必要な必須 Cookie および類似のツールを使用しています。当社は、パフォーマンス Cookie を使用して匿名の統計情報を収集することで、お客様が当社のサイトをどのように利用しているかを把握し、改善に役立てています。必須 Cookie は無効化できませんが、[カスタマイズ] または [拒否] をクリックしてパフォーマンス Cookie を拒否することはできます。

お客様が同意した場合、AWS および承認された第三者は、Cookie を使用して便利なサイト機能を提供したり、お客様の選択を記憶したり、関連する広告を含む関連コンテンツを表示したりします。すべての必須ではない Cookie を受け入れるか拒否するには、[受け入れる] または [拒否] をクリックしてください。より詳細な選択を行うには、[カスタマイズ] をクリックしてください。

アプリケーションに Amazon Location 検索を追加する

フォーカスモード
アプリケーションに Amazon Location 検索を追加する - Amazon Location Service

アプリケーションの最後のステップは、マップに検索を追加することです。この場合は、ある場所にあるアイテムを検索するリバースジオコーディング検索を追加します。

注記

Amazon Location Service では、名前または住所で検索して、地図上の場所の位置を検索することもできます。

アプリケーションに検索機能の追加
  1. 前のセクションで作成したmain.jsファイルを開きます。

  2. 図のようにmain関数を変更します。必要な変更はgreenに示されています。

    async function main() { // Create an authentication helper instance using an API key const authHelper = await amazonLocationAuthHelper.withAPIKey(apiKey); // Initialize map and Amazon Location SDK client: const map = await initializeMap(); const client = new amazonLocationClient.LocationClient({ region, ...authHelper.getLocationClientConfig(), // Provides configuration required to make requests to Amazon Location }); // On mouse click, display marker and get results: map.on("click", async function (e) { // Set up parameters for search call let params = { IndexName: placesName, Position: [e.lngLat.lng, e.lngLat.lat], Language: "en", MaxResults: "5", }; // Set up command to search for results around clicked point const searchCommand = new amazonLocationClient.SearchPlaceIndexForPositionCommand(params); try { // Make request to search for results around clicked point const data = await client.send(searchCommand); // Write JSON response data to HTML document.querySelector("#response").textContent = JSON.stringify(data, undefined, 2); // Display place label in an alert box alert(data.Results[0].Place.Label); } catch (error) { // Write JSON response error to HTML document.querySelector("#response").textContent = JSON.stringify(error, undefined, 2); // Display error in an alert box alert("There was an error searching."); } }); }

    このコードはまず、API キーを使用するように Amazon Location 認証ヘルパーを設定します。

    const authHelper = await amazonLocationAuthHelper.withAPIKey(apiKey);

    次に、その認証ヘルパーと、使用しているリージョンを使って新しい Amazon Location クライアントを作成します。

    const client = new amazonLocationClient.LocationClient({ region, ...authHelper.getLocationClientConfig(), });

    次に、ユーザーがマップ・コントロール上のスポットを選択すると、コードはそれに応答する。これは MapLibre が提供する click のイベントをキャッチすることによって行われます。

    map.on("click", async function(e) { ... });

    MapLibre click イベントは、ユーザーが選択した緯度と経度 (e.lngLat) を含むパラメータを提供します。clickイベント内では、コードによってがsearchPlaceIndexForPositionCommand作成され、指定した緯度と経度にあるエンティティが検索されます。

    // Set up parameters for search call let params = { IndexName: placesName, Position: [e.lngLat.lng, e.lngLat.lat], Language: "en", MaxResults: "5" }; // Set up command to search for results around clicked point const searchCommand = new amazonLocationClient.SearchPlaceIndexForPositionCommand(params); try { // Make request to search for results around clicked point const data = await client.send(searchCommand); ... });

    ここで、IndexName は先に作成したプレースインデックスリソースの名前、PositionLanguage を検索するための緯度と経度、Y は結果の優先言語、そして MaxResults は最大 5 つの結果のみを返すように Amazon Location に指示します。

    残りのコードはエラーをチェックし、検索結果をresponseという<pre>要素に表示し、一番上の結果をアラートボックスに表示する。

  3. (オプション) ここでquickstart.htmlファイルを保存してブラウザで開くと、マップ上の場所を選択すると、選択した場所の名前または住所が表示されます。

  4. アプリケーションの最後のステップは、MapLibre 機能を使用して、ユーザーが選択した場所にマーカーを追加することです。main 関数を次のように変更します。必要な変更はgreenに示されています。

    async function main() { // Create an authentication helper instance using an API key const authHelper = await amazonLocationAuthHelper.withAPIKey(apiKey); // Initialize map and Amazon Location SDK client const map = await initializeMap(); const client = new amazonLocationClient.LocationClient({ region, ...authHelper.getLocationClientConfig(), // Provides configuration required to make requests to Amazon Location }); // Variable to hold marker that will be rendered on click let marker; // On mouse click, display marker and get results: map.on("click", async function (e) { // Remove any existing marker if (marker) { marker.remove(); } // Render a marker on clicked point marker = new maplibregl.Marker().setLngLat([e.lngLat.lng, e.lngLat.lat]).addTo(map); // Set up parameters for search call let params = { IndexName: placesName, Position: [e.lngLat.lng, e.lngLat.lat], Language: "en", MaxResults: "5", }; // Set up command to search for results around clicked point const searchCommand = new amazonLocationClient.SearchPlaceIndexForPositionCommand(params); ...

    このコードでは marker 変数を宣言しており、ユーザーが場所を選択するたびに、選択した場所を示す Z 変数が入力される。.addTo(map);でマップに追加されると、メーカーはマップコントロールによって自動的にレンダリングされる。また、このコードは以前のメーカーをチェックして削除し、画面には一度に 1 つのマーカーしか表示されないようにします。

  5. main.js ファイルを保存し、ブラウザで quickstart.html ファイルを開きます。以前と同様にマップ上で画面移動や拡大を行うことができますが、場所を選択すると、選択した場所の詳細が表示されるようになりました。

    Map interface showing location details for 1600 Pennsylvania Ave NW, Washington, DC with JSON response.

これでクイックスタートアプリケーションが完成しました。このチュートリアルでは、次のような静的 HTML アプリケーションを作成する方法を説明しました。

  • ユーザーが操作できるマップを作成します。

  • マップイベント click を処理します。

  • Amazon Location Service API を呼び出し、特にsearchPlaceIndexForPositionを使っている場所の地図を検索します。

  • MapLibre マップコントロールを使用してメーカーを追加します。

プライバシーサイト規約Cookie の設定
© 2025, Amazon Web Services, Inc. or its affiliates.All rights reserved.