เพิ่มชุดข้อมูลลงในแผนที่

FeatureStyleFunction คือที่ที่คุณกำหนดตรรกะเพื่อจัดรูปแบบเฉพาะบางส่วนในชุดข้อมูล ซึ่งจะแสดงผล FeatureStyleOptions ตามตรรกะนี้ หากไม่จำเป็นต้องใช้ตรรกะการจัดรูปแบบ คุณจะใช้ FeatureStyleOptions เพื่อตั้งค่ารูปแบบโดยตรงได้ หน้านี้แสดงวิธีเพิ่มชุดข้อมูลลงในแผนที่และใช้การจัดรูปแบบ

สิ่งที่ต้องดำเนินการก่อน

คุณควรมีรหัสแผนที่และรูปแบบแผนที่ รวมถึงรหัสชุดข้อมูลก่อนดำเนินการต่อ

เชื่อมโยงรหัสชุดข้อมูลกับรูปแบบแผนที่

ทำตามขั้นตอนต่อไปนี้เพื่อเชื่อมโยงชุดข้อมูลกับรูปแบบแผนที่ที่คุณใช้อยู่

  1. ใน Google Cloud Console ให้ไปที่หน้าชุดข้อมูล
  2. คลิกชื่อชุดข้อมูล หน้ารายละเอียดชุดข้อมูลจะปรากฏขึ้น
  3. คลิกแท็บแสดงตัวอย่าง
  4. เลื่อนไปที่เพิ่มรูปแบบแผนที่ แล้วคลิก
    ภาพหน้าจอของปุ่ม "เพิ่มรูปแบบแผนที่"
  5. คลิกช่องทำเครื่องหมายของรูปแบบแผนที่ที่ต้องการเชื่อมโยง แล้วคลิกบันทึก

ใช้กฎรูปแบบอย่างง่าย

วิธีที่ง่ายที่สุดในการจัดรูปแบบจุดสนใจคือการส่ง 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;
  }
}

นำรูปแบบไปใช้กับเลเยอร์ฟีเจอร์ชุดข้อมูล

วิธีใช้รูปแบบในฟังก์ชันสไตล์ของฟีเจอร์

  1. รับเลเยอร์ฟีเจอร์ชุดข้อมูลโดยการเรียกใช้ map.getDatasetFeatureLayer() โดยส่งผ่านรหัสชุดข้อมูล
  2. ใช้รูปแบบดังกล่าวโดยการตั้งค่าตัวเลือกรูปแบบของฟีเจอร์ (เช่น 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);