FeatureStyleFunction
คือที่ที่คุณกำหนดตรรกะเพื่อจัดรูปแบบฟีเจอร์ในชุดข้อมูลแบบเลือก โดยจะแสดง FeatureStyleOptions
ตามตรรกะนี้ หากไม่จำเป็นต้องใช้ตรรกะการจัดรูปแบบ คุณสามารถใช้ FeatureStyleOptions
เพื่อตั้งค่าสไตล์ได้โดยตรง หน้านี้จะแสดงวิธีเพิ่มชุดข้อมูลลงในแผนที่และ
ใช้การจัดรูปแบบ
ข้อกำหนดเบื้องต้น
ก่อนดำเนินการต่อ คุณควรมีรหัสแผนที่และรูปแบบแผนที่ รวมถึงรหัสชุดข้อมูล
เชื่อมโยงรหัสชุดข้อมูลกับรูปแบบแผนที่
ทำตามขั้นตอนต่อไปนี้เพื่อเชื่อมโยงชุดข้อมูลกับรูปแบบแผนที่ที่คุณใช้
- ในคอนโซล Google Cloud ให้ไปที่หน้าชุดข้อมูล
- คลิกชื่อชุดข้อมูล หน้ารายละเอียดชุดข้อมูลจะปรากฏขึ้น
- คลิกแท็บแสดงตัวอย่าง
- เลื่อนไปที่เพิ่มสไตล์แผนที่ แล้วคลิก
- คลิกช่องทําเครื่องหมายสําหรับรูปแบบแผนที่ที่จะเชื่อมโยง แล้วคลิก บันทึก
ใช้กฎรูปแบบที่เรียบง่าย
วิธีที่ง่ายที่สุดในการจัดรูปแบบฟีเจอร์คือการส่ง FeatureStyleOptions
เพื่อกำหนด
แอตทริบิวต์รูปแบบ เช่น สี ความทึบ และความหนาของเส้น ใช้ตัวเลือกรูปแบบฟีเจอร์
กับเลเยอร์ฟีเจอร์ชุดข้อมูลโดยตรง หรือใช้ร่วมกับ
FeatureStyleFunction
TypeScript
const styleOptions = { strokeColor: 'green', strokeWeight: 2, strokeOpacity: 1, fillColor: 'green', fillOpacity: 0.3, };
JavaScript
const styleOptions = { strokeColor: "green", strokeWeight: 2, strokeOpacity: 1, fillColor: "green", fillOpacity: 0.3, };
ใช้กฎสไตล์แบบประกาศ
ใช้ FeatureStyleFunction
เพื่อตั้งกฎสไตล์แบบประกาศและนำไปใช้
กับชุดข้อมูลทั้งหมด ใช้ FeatureStyleOptions
กับฟีเจอร์ตามค่าแอตทริบิวต์ชุดข้อมูล นอกจากนี้ คุณยังส่งคืน null
จากฟังก์ชันรูปแบบฟีเจอร์
ได้ด้วย เช่น หากต้องการให้ฟีเจอร์บางส่วนยังคงมองไม่เห็น ตัวอย่างนี้แสดงฟังก์ชันรูปแบบที่ระบายสีชุดจุดตามแอตทริบิวต์ข้อมูล
TypeScript
function setStyle(/* FeatureStyleFunctionOptions */ params) { // Get the dataset feature, so we can work with all of its attributes. const datasetFeature = params.feature; // Get all of the needed dataset attributes. const furColors = datasetFeature.datasetAttributes['CombinationofPrimaryandHighlightColor']; // Apply styles. Fill is primary fur color, stroke is secondary fur color. switch (furColors) { case 'Black+': return /* FeatureStyleOptions */ { fillColor: 'black', pointRadius: 8 }; break; case 'Cinnamon+': return /* FeatureStyleOptions */ { fillColor: '#8b0000', pointRadius: 8 }; break; case 'Cinnamon+Gray': return /* FeatureStyleOptions */ { fillColor: '#8b0000', strokeColor: 'gray', pointRadius: 6 }; break; case 'Cinnamon+White': return /* FeatureStyleOptions */ { fillColor: '#8b0000', strokeColor: 'white', pointRadius: 6 }; break; case 'Gray+': return /* FeatureStyleOptions */ { fillColor: 'gray', pointRadius: 8 }; break; case 'Gray+Cinnamon': return /* FeatureStyleOptions */ { fillColor: 'gray', strokeColor: '#8b0000', pointRadius: 6 }; break; case 'Gray+Cinnamon, White': return /* FeatureStyleOptions */ { fillColor: 'silver', strokeColor: '#8b0000', pointRadius: 6 }; break; case 'Gray+White': return /* FeatureStyleOptions */ { fillColor: 'gray', strokeColor: 'white', pointRadius: 6 }; break; default: // Color not defined. return /* FeatureStyleOptions */ { fillColor: 'yellow', pointRadius: 8 }; break; } }
JavaScript
function setStyle(/* FeatureStyleFunctionOptions */ params) { // Get the dataset feature, so we can work with all of its attributes. const datasetFeature = params.feature; // Get all of the needed dataset attributes. const furColors = datasetFeature.datasetAttributes["CombinationofPrimaryandHighlightColor"]; // Apply styles. Fill is primary fur color, stroke is secondary fur color. switch (furColors) { case "Black+": return /* FeatureStyleOptions */ { fillColor: "black", pointRadius: 8 }; break; case "Cinnamon+": return /* FeatureStyleOptions */ { fillColor: "#8b0000", pointRadius: 8 }; break; case "Cinnamon+Gray": return /* FeatureStyleOptions */ { fillColor: "#8b0000", strokeColor: "gray", pointRadius: 6, }; break; case "Cinnamon+White": return /* FeatureStyleOptions */ { fillColor: "#8b0000", strokeColor: "white", pointRadius: 6, }; break; case "Gray+": return /* FeatureStyleOptions */ { fillColor: "gray", pointRadius: 8 }; break; case "Gray+Cinnamon": return /* FeatureStyleOptions */ { fillColor: "gray", strokeColor: "#8b0000", pointRadius: 6, }; break; case "Gray+Cinnamon, White": return /* FeatureStyleOptions */ { fillColor: "silver", strokeColor: "#8b0000", pointRadius: 6, }; break; case "Gray+White": return /* FeatureStyleOptions */ { fillColor: "gray", strokeColor: "white", pointRadius: 6, }; break; default: // Color not defined. return /* FeatureStyleOptions */ { fillColor: "yellow", pointRadius: 8 }; break; } }
ใช้สไตล์กับเลเยอร์ฟีเจอร์ของชุดข้อมูล
วิธีใช้รูปแบบในฟังก์ชันรูปแบบฟีเจอร์
- รับเลเยอร์ฟีเจอร์ของชุดข้อมูลโดยการเรียก
map.getDatasetFeatureLayer()
, ส่งรหัสชุดข้อมูล - ใช้สไตล์โดยการตั้งค่าตัวเลือกสไตล์ฟีเจอร์ (เช่น
styleOptions
) หรือฟังก์ชัน (เช่นsetStyle
) ในเลเยอร์ชุดข้อมูล
TypeScript
const datasetLayer = map.getDatasetFeatureLayer(datasetId); datasetLayer.style = styleOptions;
JavaScript
const datasetLayer = map.getDatasetFeatureLayer(datasetId); datasetLayer.style = styleOptions;
นำการจัดรูปแบบออกจากเลเยอร์
หากต้องการนำการจัดรูปแบบออกจากเลเยอร์ ให้ตั้งค่า style
เป็น null
featureLayer.style = null;
นอกจากนี้ คุณยังแสดงผล null
จากฟังก์ชันรูปแบบฟีเจอร์ได้ด้วย เช่น หากต้องการให้ฟีเจอร์บางส่วนยังคงมองไม่เห็น
เพิ่มข้อความระบุแหล่งที่มา
แผนที่ของคุณต้องแสดงการระบุแหล่งที่มาที่จำเป็นเมื่อแสดงชุดข้อมูลที่อัปโหลดใน Google Maps ข้อความระบุแหล่งที่มาต้องไม่บดบังหรือรบกวนโลโก้ Google
วิธีหนึ่งในการเพิ่มข้อความระบุแหล่งที่มาคือการใช้การควบคุมที่กำหนดเอง เพื่อวาง HTML ที่กำหนดเองในตำแหน่งมาตรฐานบนแผนที่ ตัวอย่างโค้ดต่อไปนี้แสดงฟังก์ชันที่สร้างการควบคุมที่กำหนดเองดังกล่าวโดยอัตโนมัติ
TypeScript
function createAttribution(map) { const attributionLabel = document.createElement('div'); // Define CSS styles. attributionLabel.style.backgroundColor = '#fff'; attributionLabel.style.opacity = '0.7'; attributionLabel.style.fontFamily = 'Roboto,Arial,sans-serif'; attributionLabel.style.fontSize = '10px'; attributionLabel.style.padding = '2px'; attributionLabel.style.margin = '2px'; attributionLabel.textContent = 'Data source: NYC Open Data'; return attributionLabel; }
JavaScript
function createAttribution(map) { const attributionLabel = document.createElement("div"); // Define CSS styles. attributionLabel.style.backgroundColor = "#fff"; attributionLabel.style.opacity = "0.7"; attributionLabel.style.fontFamily = "Roboto,Arial,sans-serif"; attributionLabel.style.fontSize = "10px"; attributionLabel.style.padding = "2px"; attributionLabel.style.margin = "2px"; attributionLabel.textContent = "Data source: NYC Open Data"; return attributionLabel; }
เมื่อกำหนดการควบคุมแล้ว คุณจะเพิ่มการควบคุมลงในแผนที่ได้ในเวลาเริ่มต้น ดังที่แสดงที่นี่
TypeScript
// Create an attribution DIV and add the attribution to the map. const attributionDiv = document.createElement('div'); const attributionControl = createAttribution(map); attributionDiv.appendChild(attributionControl); map.controls[google.maps.ControlPosition.LEFT_BOTTOM].push(attributionDiv);
JavaScript
// Create an attribution DIV and add the attribution to the map. const attributionDiv = document.createElement("div"); const attributionControl = createAttribution(map); attributionDiv.appendChild(attributionControl); map.controls[google.maps.ControlPosition.LEFT_BOTTOM].push(attributionDiv);