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

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

ข้อกำหนดเบื้องต้น

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

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

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

  1. ในคอนโซล Google Cloud ให้ไปที่หน้าชุดข้อมูล
  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);