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

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);