境界線ポリゴンのスタイルを設定する

概要

対象物レイヤの style プロパティを google.maps.FeatureStyleFunction に設定すると、境界線ポリゴンの塗りつぶしとストロークのスタイルを設定できます。これにより、色、不透明度、線の太さのスタイル属性を定義できます。

ポリゴンのスタイルを設定するには、style プロパティを google.maps.FeatureStyleFunction に設定します。スタイル関数では、対象物レイヤ上の個々のポリゴンのスタイルを設定するロジックを定義します。featureLayer.style を設定すると、影響を受ける対象物レイヤ内のすべての対象物でスタイル関数が実行されます。この関数は、スタイル プロパティの設定時に適用されます。スタイルを更新するには、スタイル プロパティを再度設定する必要があります。

スタイル関数を対象物に適用した場合、常に一貫した結果が返されます。たとえば、対象物一式をランダムに色付けした場合、対象物のスタイル関数ではランダムな部分は実行されません。予期しない結果を招くためです。

この関数はレイヤ内のすべての対象物に対して実行されるため、最適化が重要です。レンダリング時間に影響しないようにするには、次の点に気をつけます。

  • 必要なレイヤのみを有効にする。
  • レイヤが不要になった場合は、stylenull に設定する。

[地域区分] 対象物レイヤ内にあるポリゴンのスタイルを設定する手順は次のとおりです。

  1. まだ行っていない場合は、スタートガイドの手順に沿って新しいマップ ID と地図のスタイルを作成します。必ず [地域区分] 対象物レイヤを有効にしてください。
  2. 地図を初期化する際に、[地域区分] 対象物レイヤへの参照を取得します。

    featureLayer = map.getFeatureLayer("LOCALITY");
  3. google.maps.FeatureStyleFunction タイプのスタイル定義を作成します。

  4. 対象物レイヤの style プロパティを FeatureStyleFunction に設定します。次の例は、プレイス ID と一致する google.maps.Feature にのみスタイルを適用する関数を定義する方法を示しています。

    TypeScript

    // Define a style with purple fill and border.
    //@ts-ignore
    const featureStyleOptions: google.maps.FeatureStyleOptions = {
      strokeColor: '#810FCB',
      strokeOpacity: 1.0,
      strokeWeight: 3.0,
      fillColor: '#810FCB',
      fillOpacity: 0.5
    };
    
    // Apply the style to a single boundary.
    //@ts-ignore
    featureLayer.style = (options: { feature: { placeId: string; }; }) => {
      if (options.feature.placeId == 'ChIJ0zQtYiWsVHkRk8lRoB1RNPo') { // Hana, HI
        return featureStyleOptions;
      }
    };

    JavaScript

    // Define a style with purple fill and border.
    //@ts-ignore
    const featureStyleOptions = {
      strokeColor: "#810FCB",
      strokeOpacity: 1.0,
      strokeWeight: 3.0,
      fillColor: "#810FCB",
      fillOpacity: 0.5,
    };
    
    // Apply the style to a single boundary.
    //@ts-ignore
    featureLayer.style = (options) => {
      if (options.feature.placeId == "ChIJ0zQtYiWsVHkRk8lRoB1RNPo") {
        // Hana, HI
        return featureStyleOptions;
      }
    };

指定したプレイス ID が見つからない場合、または選択した対象物タイプと一致しない場合、スタイルは適用されません。たとえば、「New York City」のプレイス ID と一致する POSTAL_CODE レイヤのスタイル設定を試みても、スタイルは適用されません。

レイヤからスタイル設定を削除する

レイヤからスタイル設定を削除するには、次のように stylenull に設定します。

featureLayer.style = null;

プレイス ID を検索して対象物をターゲットに設定する

地域のデータを取得する方法は次のとおりです。

地域の範囲のビューアを使用して、サポートされているすべての地域で利用可能な Google 境界線を確認します。

範囲は地域によって異なります。詳しくは、Google 境界線の範囲をご覧ください。

地名は、米国地名委員会米国の地名データファイルなど、さまざまな情報源から入手できます。

コードサンプルの全文

TypeScript

let map: google.maps.Map;
//@ts-ignore
let featureLayer;

function initMap() {
  map = new google.maps.Map(document.getElementById('map') as HTMLElement, {
    center: { lat: 20.773, lng: -156.01 }, // Hana, HI
    zoom: 12,
    // In the cloud console, configure this Map ID with a style that enables the
    // "Locality" feature layer.
    mapId: 'a3efe1c035bad51b', // <YOUR_MAP_ID_HERE>,
  });

  //@ts-ignore
  featureLayer = map.getFeatureLayer('LOCALITY');

  // Define a style with purple fill and border.
  //@ts-ignore
  const featureStyleOptions: google.maps.FeatureStyleOptions = {
    strokeColor: '#810FCB',
    strokeOpacity: 1.0,
    strokeWeight: 3.0,
    fillColor: '#810FCB',
    fillOpacity: 0.5
  };

  // Apply the style to a single boundary.
  //@ts-ignore
  featureLayer.style = (options: { feature: { placeId: string; }; }) => {
    if (options.feature.placeId == 'ChIJ0zQtYiWsVHkRk8lRoB1RNPo') { // Hana, HI
      return featureStyleOptions;
    }
  };

}

declare global {
  interface Window {
    initMap: () => void;
  }
}
window.initMap = initMap;

JavaScript

let map;
//@ts-ignore
let featureLayer;

function initMap() {
  map = new google.maps.Map(document.getElementById("map"), {
    center: { lat: 20.773, lng: -156.01 },
    zoom: 12,
    // In the cloud console, configure this Map ID with a style that enables the
    // "Locality" feature layer.
    mapId: "a3efe1c035bad51b", // <YOUR_MAP_ID_HERE>,
  });
  //@ts-ignore
  featureLayer = map.getFeatureLayer("LOCALITY");

  // Define a style with purple fill and border.
  //@ts-ignore
  const featureStyleOptions = {
    strokeColor: "#810FCB",
    strokeOpacity: 1.0,
    strokeWeight: 3.0,
    fillColor: "#810FCB",
    fillOpacity: 0.5,
  };

  // Apply the style to a single boundary.
  //@ts-ignore
  featureLayer.style = (options) => {
    if (options.feature.placeId == "ChIJ0zQtYiWsVHkRk8lRoB1RNPo") {
      // Hana, HI
      return featureStyleOptions;
    }
  };
}

window.initMap = initMap;

CSS

/*
 * Always set the map height explicitly to define the size of the div element
 * that contains the map.
 */
#map {
  height: 100%;
}

/*
 * Optional: Makes the sample page fill the window.
 */
html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}

HTML

<html>
  <head>
    <title>Boundaries Simple</title>
    <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>

    <link rel="stylesheet" type="text/css" href="./style.css" />
    <script type="module" src="./index.js"></script>
  </head>
  <body>
    <div id="map"></div>

    <!--
      The `defer` attribute causes the callback to execute after the full HTML
      document has been parsed. For non-blocking uses, avoiding race conditions,
      and consistent behavior across browsers, consider loading using Promises.
      See https://developers.google.com/maps/documentation/javascript/load-maps-js-api
      for more information.
      -->
    <script
      src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&callback=initMap&v=beta"
      defer
    ></script>
  </body>
</html>

サンプルを試す