概要
境界線ポリゴンの塗りつぶしとストロークをスタイル設定するには、FeatureStyleOptions を使ってスタイル属性を定義し、地図のレイヤの style プロパティを、スタイル設定ロジックが格納された google.maps.FeatureStyleFunction 関数に設定します。
次のサンプル地図では、単一の地域の境界線ポリゴンをハイライトしています。
境界線対象物にスタイルを適用するには、style プロパティを、スタイル設定ロジックを格納できる google.maps.FeatureStyleFunction 関数に設定します。スタイル関数は、適用先の地図のレイヤに含まれるすべての対象物に実行されます。また、関数が適用されるのは、style プロパティを設定したときです。スタイルを更新するには、style プロパティを設定し直す必要があります。
地図のレイヤのすべての対象物を均一にスタイル設定する場合は、style プロパティを google.maps.FeatureStyleOptions に設定します。この場合、ロジックが不要なので、対象物スタイル関数を使う必要はありません。
スタイル関数は、対象物に適用した際に、常に同じ結果を返すように定義する必要があります。たとえば、一連の対象物をランダムに色付けしたい場合は、予期しない結果を招かないよう、ランダム処理は対象物スタイル関数の外で行います。
この関数はレイヤ内のすべての対象物に対して実行されるため、最適化が重要です。レンダリング時間に影響しないようにするには、次の点に気をつけます。
- 必要なレイヤのみを有効にする。
 - レイヤが不要になった場合は、
styleをnullに設定する。 
地図の [地域区分] レイヤ内にあるポリゴンのスタイルを設定する手順は次のとおりです。
- まだ行っていない場合は、スタートガイドの手順に沿って新しいマップ ID と地図のスタイルを作成します。必ず [地域区分] レイヤを有効にしてください。
 地図を初期化する際に、[地域区分] レイヤへの参照を取得します。
google.maps.FeatureStyleFunctionタイプのスタイル定義を作成します。地図のレイヤの
styleプロパティをFeatureStyleFunctionに設定します。次の例は、プレイス ID と一致するgoogle.maps.Featureにのみスタイルを適用する関数を定義する方法を示しています。TypeScript
// Define a style with purple fill and border. 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. featureLayer.style = (options: { feature: { placeId: string; }; }) => { if (options.feature.placeId == 'ChIJ0zQtYiWsVHkRk8lRoB1RNPo') { // Hana, HI return featureStyleOptions; } };
JavaScript
// Define a style with purple fill and border. const featureStyleOptions = { strokeColor: '#810FCB', strokeOpacity: 1.0, strokeWeight: 3.0, fillColor: '#810FCB', fillOpacity: 0.5 }; // Apply the style to a single boundary. featureLayer.style = (options) => { if (options.feature.placeId == 'ChIJ0zQtYiWsVHkRk8lRoB1RNPo') { // Hana, HI return featureStyleOptions; } };
指定したプレイス ID が見つからない場合、または選択した対象物タイプと一致しない場合、スタイルは適用されません。たとえば、「New York City」のプレイス ID と一致する POSTAL_CODE レイヤのスタイル設定を試みても、スタイルは適用されません。
レイヤからスタイル設定を削除する
レイヤからスタイル設定を削除するには、次のように style を null に設定します。
featureLayer.style = null;
プレイス ID を検索して対象物をターゲットに設定する
地域のプレイス ID を取得する方法:
- Places API 群とジオコーディングを利用して、目的の地域を名前で検索し、指定した境界内の地域のプレイス ID を取得します。
 - クリック イベントからデータを取得します。この場合、クリックされた地域に対応する対象物が返されるため、その対象物を通してプレイス ID、表示名、対象物タイプカテゴリを参照できます。
 
提供状況は地域によって異なります。詳しくは、Google の境界線の範囲をご覧ください。
地名は、米国地名委員会や米国の地名データファイルなど、さまざまな情報源から入手できます。
コードサンプルの全文
TypeScript
let featureLayer; async function initMap() { // Request needed libraries. await google.maps.importLibrary("maps") as google.maps.MapsLibrary; // Get the gmp-map element. const mapElement = document.querySelector( "gmp-map" ) as google.maps.MapElement; // Get the inner map. const innerMap = mapElement.innerMap; // Get the feature layer. featureLayer = innerMap.getFeatureLayer(google.maps.FeatureType.LOCALITY); // Define a style with purple fill and border. 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. featureLayer.style = (options: { feature: { placeId: string; }; }) => { if (options.feature.placeId == 'ChIJ0zQtYiWsVHkRk8lRoB1RNPo') { // Hana, HI return featureStyleOptions; } }; } initMap();
JavaScript
let featureLayer; async function initMap() { // Request needed libraries. await google.maps.importLibrary("maps"); // Get the gmp-map element. const mapElement = document.querySelector("gmp-map"); // Get the inner map. const innerMap = mapElement.innerMap; // Get the feature layer. featureLayer = innerMap.getFeatureLayer(google.maps.FeatureType.LOCALITY); // Define a style with purple fill and border. const featureStyleOptions = { strokeColor: '#810FCB', strokeOpacity: 1.0, strokeWeight: 3.0, fillColor: '#810FCB', fillOpacity: 0.5 }; // Apply the style to a single boundary. featureLayer.style = (options) => { if (options.feature.placeId == 'ChIJ0zQtYiWsVHkRk8lRoB1RNPo') { // Hana, HI return featureStyleOptions; } }; } initMap();
CSS
/* * Always set the map height explicitly to define the size of the div element * that contains the map. */ gmp-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>
    <link rel="stylesheet" type="text/css" href="./style.css" />
    <script type="module" src="./index.js"></script>
    <!-- prettier-ignore -->
    <script>(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})
        ({key: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "weekly"});</script>
  </head>
  <body>
    <gmp-map center="20.773,-156.01" zoom="12" map-id="8b37d7206ccf0121d4414bb0">
  </body>
</html>