ภาพรวม
หากต้องการจัดรูปแบบการเติมและเส้นขอบสำหรับรูปหลายเหลี่ยมขอบเขต ให้ใช้ FeatureStyleOptions
เพื่อกำหนดแอตทริบิวต์รูปแบบ และตั้งค่าพร็อพเพอร์ตี้ style ในเลเยอร์ฟีเจอร์เป็น google.maps.FeatureStyleFunction ซึ่งมีตรรกะการจัดรูปแบบ
แผนที่ตัวอย่างต่อไปนี้แสดงการไฮไลต์รูปหลายเหลี่ยมขอบเขตสำหรับภูมิภาคเดียว
ใช้รูปแบบกับฟีเจอร์ขอบเขตโดยตั้งค่าพร็อพเพอร์ตี้ style เป็น
google.maps.FeatureStyleFunction ซึ่งอาจมีตรรกะการจัดรูปแบบ ฟังก์ชันสไตล์
จะทำงานกับทุกฟีเจอร์ในเลเยอร์ฟีเจอร์ที่ได้รับผลกระทบ และจะใช้
ในเวลาที่คุณตั้งค่าพร็อพเพอร์ตี้สไตล์ หากต้องการอัปเดต คุณต้องตั้งค่าพร็อพเพอร์ตี้สไตล์
อีกครั้ง
หากต้องการจัดรูปแบบฟีเจอร์ทั้งหมดสำหรับเลเยอร์ฟีเจอร์อย่างสม่ำเสมอ ให้ตั้งค่าพร็อพเพอร์ตี้ style เป็น
google.maps.FeatureStyleOptions ในกรณีนี้ คุณไม่จำเป็นต้องใช้ฟังก์ชันรูปแบบฟีเจอร์ เนื่องจากไม่จำเป็นต้องใช้ตรรกะ
ฟังก์ชันสไตล์ควรแสดงผลลัพธ์ที่สอดคล้องกันเสมอเมื่อใช้กับฟีเจอร์ เช่น หากต้องการกำหนดสีแบบสุ่มให้กับชุดฟีเจอร์ ส่วนแบบสุ่มไม่ควรอยู่ในฟังก์ชันรูปแบบฟีเจอร์ เนื่องจาก จะทำให้เกิดผลลัพธ์ที่ไม่ต้องการ
เนื่องจากฟังก์ชันนี้ทำงานกับทุกฟีเจอร์ในเลเยอร์ การเพิ่มประสิทธิภาพจึงเป็นสิ่งสำคัญ วิธีหลีกเลี่ยงไม่ให้ส่งผลต่อเวลาในการแสดงผล
- เปิดใช้เฉพาะเลเยอร์ที่คุณต้องการ
 - ตั้งค่า 
styleเป็นnullเมื่อเลิกใช้เลเยอร์ 
หากต้องการจัดรูปแบบรูปหลายเหลี่ยมในเลเยอร์ฟีเจอร์ของสถานที่ ให้ทำตามขั้นตอนต่อไปนี้
- หากยังไม่ได้ดำเนินการ ให้ทำตามขั้นตอนในเริ่มต้นใช้งาน เพื่อสร้างรหัสแผนที่และรูปแบบแผนที่ใหม่ อย่าลืมเปิดใช้ฟีเจอร์เลเยอร์สถานที่
 รับการอ้างอิงไปยังเลเยอร์ฟีเจอร์ของสถานที่เมื่อแผนที่เริ่มต้น
สร้างคำจำกัดความของสไตล์ประเภท
google.maps.FeatureStyleFunctionตั้งค่าพร็อพเพอร์ตี้
styleในเลเยอร์ฟีเจอร์เป็นFeatureStyleFunctionตัวอย่างต่อไปนี้แสดงการกำหนดฟังก์ชันเพื่อใช้สไตล์กับ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; } };
หากไม่พบรหัสสถานที่ที่ระบุ หรือไม่ตรงกับประเภทฟีเจอร์ที่เลือก ระบบจะไม่ใช้สไตล์ เช่น
การพยายามจัดรูปแบบPOSTAL_CODEเลเยอร์ที่ตรงกับรหัสสถานที่ของ "นิวยอร์ก
ซิตี" จะทำให้ไม่มีการใช้รูปแบบ
นำการจัดรูปแบบออกจากเลเยอร์
หากต้องการนำการจัดรูปแบบออกจากเลเยอร์ ให้ตั้งค่า style เป็น null
featureLayer.style = null;
ค้นหารหัสสถานที่เพื่อกำหนดเป้าหมายฟีเจอร์
วิธีรับรหัสสถานที่สำหรับภูมิภาค
- ใช้ Places API และ Geocoding เพื่อค้นหาภูมิภาคตามชื่อ และรับรหัสสถานที่สำหรับภูมิภาคภายในขอบเขตที่ระบุ
 - รับข้อมูลจากเหตุการณ์คลิก ซึ่งจะแสดงฟีเจอร์ที่สอดคล้องกับภูมิภาคที่คลิก ซึ่งให้สิทธิ์เข้าถึงรหัสสถานที่ ชื่อที่แสดง และหมวดหมู่ประเภทฟีเจอร์
 
ความครอบคลุมจะแตกต่างกันไปตามภูมิภาค ดูรายละเอียดได้ที่ความครอบคลุมของขอบเขตของ Google
ชื่อทางภูมิศาสตร์มาจากหลายแหล่ง เช่น USGS Board on Geographic Names และU.S. Gazetteer Files
โค้ดตัวอย่างที่สมบูรณ์
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>