空港へのナビゲーションの改善

ターミナル マーカーが表示された空港の地図

空港は複数のターミナルからなる大規模な複合施設であり、時間どおりに到着、出発するには正確なナビゲーションが不可欠です。Google Maps Geocoding API は通常、座標(緯度/経度)を返します。ほとんどの場合、これは大規模な空港施設の中心点になります。このアプリは、空港内の特定のターミナルや乗降場所など、大規模な複合施設内の正確な位置を特定して可視化できるように設計されたインタラクティブなツールです。

イメージ

これを実現する方法は次のとおりです。

空港/会場の検索: ユーザーはまず、Google Places Autocomplete 入力を使用して、主要な場所(「インディラ ガンディー国際空港」など)を検索します。この入力はインドに限定されています。

サブエリアの検出: メインの場所が選択されると、スクリプトは Google Places API を使用して詳細情報を取得します。重要なのは、その場所に関連付けられている「サブ目的地」(ターミナル 1、ターミナル 3、特定のゲートなど、Google のデータで利用可能な場合)が含まれていることです。

ビジュアル マッピング: スクリプトは Geocoding API を使用して、メインの場所とそのサブ目的地の座標を検索します。

次に、メインの場所を表示し、特定された各サブ目的地に、クリック可能な個別のマーカー(青い円)を地図上に配置します。

正確な識別: サブ目的地のマーカーをクリックすると、マーカーがハイライト表示(緑色に変化)され、名前やその他の詳細情報(住所やタイプなど)を示す情報ウィンドウが開きます。これにより、ユーザーは正しい特定のポイントを選択したことを確認できます。コンテキスト ビュー: 地図のビュー(fitBounds)が自動的に調整され、関連するすべてのマーカー(メインの場所とサブの目的地)が明確に表示されます。

空港ナビゲーション アプリで使用されている Google Maps Platform API

このドキュメントでは、提供されている「空港へのナビ」デモアプリで使用されている主要な Google Maps Platform API とそのパラメータについて説明します。このアプリは、複数のサービスを活用して、地図表示、Place Search、詳細な場所情報、高度な位置情報に関する分析情報を提供します。

1. 地図の初期化と表示

このアプリケーションの基盤は、インタラクティブな地図そのものです。

  • 使用する API: google.maps.Map(Maps JavaScript API)
  • 目的: ウェブページにインタラクティブな地図を作成して表示する。
  • 主なパラメータ:
    • center: 地図の初期の地理的中心を定義します。このアプリでは、最初はデリーの座標({ lat: 28.461835685621395, lng: 77.05004035761647 })に設定されています。
    • zoom: 地図の初期ズームレベルを設定します。DEFAULT_ZOOM_LEVEL(15)はクローズアップ ビューに使用されます。
    • mapId: Google Cloud コンソールで構成された地図のスタイルの一意の識別子。

2. Place Search と Place Autocomplete

検索バーの機能は Places API を利用しています。

  • 使用する API: google.maps.places.Autocomplete(Maps JavaScript API の Places ライブラリ)
  • 目的: ユーザーが入力した地理検索の予測テキストを補完し、空港などの関連する場所を提案します。
  • 主なパラメータ:
    • input: ユーザーがクエリを入力する HTML 入力要素(#search-input)。
    • componentRestrictions: 検索結果を特定の国にフィルタします。ここで、{ country: 'in' } は結果をインドに制限します。
    • fields: 選択した場所について返すデータ フィールドを指定します。['place_id'] は、最初に場所の固有識別子のみを取得するために使用され、データ移転を最適化します。
  • 予測入力の使用方法
    // Initialize Autocomplete
    const autocomplete = new google.maps.places.Autocomplete(input, {
      componentRestrictions: { country: 'in' },
      fields: ['place_id'],
    });

    // Add listener to the Autocomplete
    autocomplete.addListener('place_changed', async () => {
      const place = autocomplete.getPlace();
      if (!place.place_id) {
        return;
      }
      // Once a place is selected, fetch details
      await getPlaceDetails(place.place_id);
    });

3. 詳細な場所情報の取得とサブ目的地の処理

予測入力の候補から場所が選択されると、より詳細な情報が取得されます。

  • 使用する API: Places API(https://places.googleapis.com/v1/places/{placeId} への直接 fetch 呼び出しを使用)
  • 目的: 特定の場所に関する詳細情報(表示名、住所、タイプ、特に重要な subDestinations(空港などの大規模な複合施設内の個々のターミナルや重要なエリアなど))を取得します。
  • URL の主なパラメータ:
    1. {placeId}: 選択した場所の固有識別子。
    2. fields: 取得するデータ フィールドを正確に指定します。アプリは id、displayName、subDestinations、types、formattedAddress をリクエストします。これは、費用を管理し、必要なデータのみを受信するために不可欠です。
  • 場所を指定して subDestinations を取得する方法
async function getPlaceDetails(placeId) {
  // Construct the URL for the Places API (v1) details endpoint
  // The 'fields' parameter is crucial for requesting subDestinations
  const url = `https://places.googleapis.com/v1/places/${placeId}?key=YOUR_API_KEY&fields=id,displayName,subDestinations,types,formattedAddress`;

  const response = await fetch(url);
  const data = await response.json();

  // Accessing subDestinations from the Places API response
  if (data.subDestinations && data.subDestinations.length > 0) {
    for (const subDestination of data.subDestinations) {
      // Each subDestination object contains an 'id' and 'displayName'
      console.log(`Sub-destination ID: ${subDestination.id}`);
      console.log(`Sub-destination Name: ${subDestination.displayName?.text}`);
      // This subDestination.id is then used in a geocoding call (as shown in section 4)
    }
  }
}

**Handling `subDestinations`:** When the Places API returns
`subDestinations`, the application initiates a process for each one:
1.  **Geocoding:** It uses the `google.maps.Geocoder` to convert
    each `subDestination.id` into its precise geographical coordinates
    (`lat`, `lng`).
1.  **Marker Placement:** A distinct marker is added to the map for
    each sub-destination. These markers are styled with a blue circle icon
    to differentiate them.
1.  **Map Bounds Adjustment:** The `google.maps.LatLngBounds` object
    is used to dynamically expand the map's view to encompass all retrieved
    sub-destinations, verifying they are all visible within the current map
    frame.
1.  **Interactive Information Window:** A `click` listener is
    attached to each sub-destination marker. When clicked, the marker's
    icon changes to green, and an `InfoWindow` appears, displaying the
    sub-destination's name, address, and types. This provides immediate,
    detailed context to the user.

4. ジオコーディングとリバース ジオコーディング: サブ目的地の詳細を取得する

このアプリケーションでは、主に 2 つの目的でジオコーディングを使用しています。1 つはプレイス ID を座標に変換すること、もう 1 つは座標を場所の詳細に変換することです。このセクションでは、ジオコーディングを使用してサブ目的地の詳細情報を取得する方法について説明します。

  • 使用する API: google.maps.Geocoder(Maps JavaScript API から)と Geocoding API(https://maps.googleapis.com/maps/api/geocode/json への直接の fetch 呼び出しを介して)
  • 目的:
    • google.maps.Geocoder: placeId(Autocomplete または Places API から取得)を地理座標(lat、lng)とビューポートに変換するために使用されます。これにより、選択した場所とそのサブ目的地を地図の中心に正しく表示し、ズームできます。
    • Geocoding API(fetch): リバース ジオコーディング(緯度と経度を人が読める住所に変換する)と、建物の外形やナビゲーション ポイントなどの高度な位置情報の取得に使用されます。
  • 主なパラメータ:
    • google.maps.Geocoder.geocode():
      • placeId: ジオコーディングするプレイス ID。
      • location: リバース ジオコーディング用の LatLng オブジェクト。
    • Geocoding API fetch 呼び出し:
      • latlng: リバース ジオコーディングの緯度と経度の座標。
      • extra_computations=BUILDING_AND_ENTRANCES: この重要なパラメータは、追加のデータ(具体的には建物のフットプリントと入口情報)をリクエストします。このデータは、建物の輪郭とナビゲーション ポイントを表示するために使用されます。

subDestination ID を使用して詳細情報(場所、フォーマットされた住所、タイプなど)を取得する方法

function geocodeAndAddMarker(subDestination, bounds) {
  return new Promise((resolve, reject) => {
    const geocoder = new google.maps.Geocoder();
    // Using the subDestination.id to geocode and get location details
    geocoder.geocode({ placeId: subDestination.id }, (results, status) => {
      if (status === "OK" && results[0]) {
        const location = results[0].geometry.location;
        const displayName = subDestination.displayName?.text || "Sub-destination";
        const formattedAddress = results[0].formatted_address; // Further detail from Geocoding
        const types = results[0].types; // Further detail from Geocoding

        const marker = new google.maps.Marker({
          map: map,
          position: location,
          title: displayName,
          icon: {
            path: google.maps.SymbolPath.CIRCLE,
            fillColor: 'blue',
            fillOpacity: 0.6,
            strokeWeight: 0,
            scale: 8
          }
        });

        marker.addListener('click', () => {
          marker.setIcon({
            path: google.maps.SymbolPath.CIRCLE,
            fillColor: 'green',
            fillOpacity: 0.6,
            strokeWeight: 0,
            scale: 8
          });
          const infowindow = new google.maps.InfoWindow({
            content: `<b>${displayName}</b><br><p>Address: ${formattedAddress}</p><p>Types: ${types.join(', ')}</p>`,
          });
          infowindow.open(map, marker);
        });
        bounds.extend(location);
        resolve(true);
      } else {
        reject(new Error(`Geocoding failed for placeId: ${subDestination.id}`));
      }
    });
  });
}

5. マーカーの表示

マーカーは、地図上の特定の場所をハイライト表示するために使用されます。

  • 使用する API: google.maps.Marker(Maps JavaScript API)と google.maps.marker.AdvancedMarkerElement(google.maps.marker.PinElement を使用、Maps JavaScript API の Marker Library)
  • 目的:
    • google.maps.Marker: 最初のドラッグ可能なマーカー(提供されたコードでは draggable が false に設定されていますが、これはその機能の一部です)と、セクション 3 で説明されている基本的なサブ目的地のマーカーに使用されます。
    • AdvancedMarkerElement と PinElement: 視覚的に区別しやすいナビゲーション ポイント マーカーに使用され、マーカーのピンのカスタム スタイルを設定できます。
  • 主なパラメータ:
    • position: マーカーを配置する LatLng 座標。
    • map: マーカーが表示される地図インスタンス。
    • title: マーカーにカーソルを合わせたときに表示されるテキスト。
    • icon: google.maps.Marker のカスタム アイコン(カスタムカラーの google.maps.SymbolPath.CIRCLE など)を使用できます。
    • content: AdvancedMarkerElement の場合、カスタム HTML コンテンツを埋め込むことができます。これには、スタイル設定済みのピンの PinElement も含まれます。
    • PinElement パラメータ: background、borderColor、glyphColor、scale(ビジュアル カスタマイズ用)。

6. 建物の輪郭を表示する

アプリケーションは、建物のフットプリントを視覚的に表現できます。

  • 使用する API: google.maps.Data(Maps JavaScript API)
  • 目的: 建物の輪郭などの地理データを表示する(Geocoding API の extra_computations から GeoJSON display_polygon として返されます)。
  • 主なパラメータ:
    • map: データレイヤが適用される地図インスタンス。
    • style: GeoJSON フィーチャーの視覚的な外観を定義します(例: strokeColor、fillColor、fillOpacity)。
    • addGeoJson(): GeoJSON データをレイヤに追加するメソッド。

7. 地図の境界とズーム

マップビューに関連するすべての場所が含まれていることを確認します。

  • 使用する API: google.maps.LatLngBounds(Maps JavaScript API)
  • 目的: 地図のビューポートを動的に調整して、一連の地理的位置(メインの場所とそのすべてのサブ目的地など)に合わせます。
  • 主なメソッド:
    • extend(location): LatLng ポイントを境界に追加し、必要に応じて境界を拡張します。
    • fitBounds(bounds): 地図の中心とズームレベルを調整して、LatLngBounds オブジェクトで定義されたエリア全体を表示します。

これらの Google Maps Platform API を組み合わせることで、場所の検索、詳細の表示、関連する地理情報(サブ目的地や建物の輪郭など)の可視化など、包括的でインタラクティブなエクスペリエンスを提供できます。

実装に関する考慮事項 この機能はすべての空港エリアで動作するわけではなく、(空港ターミナルの)データの可用性に依存します。

リソース Geocoding API Places API Maps Javascript API

作成者: