Thêm tập dữ liệu vào bản đồ

FeatureStyleFunction là nơi bạn xác định logic để chọn lọc các đối tượng trong một tập dữ liệu. Phương thức này trả về FeatureStyleOptions dựa trên logic này. Nếu không cần logic định kiểu, bạn có thể sử dụng FeatureStyleOptions để trực tiếp đặt kiểu. Trang này cho bạn biết cách thêm tập dữ liệu vào bản đồ và áp dụng kiểu.

Điều kiện tiên quyết

Trước khi tiếp tục, bạn cần có một mã bản đồ và kiểu bản đồ, cũng như một mã tập dữ liệu.

Liên kết mã tập dữ liệu với kiểu bản đồ

Hãy làm theo các bước sau để liên kết tập dữ liệu với kiểu bản đồ mà bạn đang sử dụng:

  1. Trong Google Cloud Console, hãy truy cập vào trang Tập dữ liệu.
  2. Nhấp vào tên của tập dữ liệu. Trang Thông tin chi tiết về tập dữ liệu sẽ xuất hiện.
  3. Nhấp vào thẻ Xem trước.
  4. Di chuyển xuống, rồi nhấp vào THÊM KIỂU BẢN ĐỒ.
    Ảnh chụp màn hình nút THÊM KIỂU BẢN ĐỒ.
  5. Nhấp vào(các) hộp đánh dấu tương ứng với(các) Kiểu bản đồ bạn muốn liên kết, sau đó nhấp vào LƯU.

Sử dụng quy tắc kiểu đơn giản

Cách đơn giản nhất để tạo kiểu cho các đối tượng là truyền FeatureStyleOptions để xác định các thuộc tính kiểu, chẳng hạn như màu sắc, độ mờ và độ đậm của đường kẻ. Áp dụng các tuỳ chọn kiểu tính năng trực tiếp cho một lớp tính năng của tập dữ liệu hoặc sử dụng các tuỳ chọn đó cùng với 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,
};

Sử dụng quy tắc kiểu khai báo

Sử dụng FeatureStyleFunction để đặt các quy tắc kiểu theo cách khai báo và áp dụng các quy tắc đó trên toàn bộ tập dữ liệu. Áp dụng FeatureStyleOptions cho một tính năng dựa trên giá trị thuộc tính tập dữ liệu. Ví dụ: bạn cũng có thể trả về null từ hàm kiểu tính năng, chẳng hạn như nếu bạn muốn duy trì trạng thái ẩn của một tập hợp con các tính năng. Ví dụ này cho thấy một hàm định kiểu tô màu một tập hợp các điểm dựa trên thuộc tính dữ liệu:

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

Áp dụng kiểu cho lớp đối tượng tập dữ liệu

Cách áp dụng kiểu trong hàm kiểu tính năng:

  1. Tải lớp tính năng của tập dữ liệu bằng cách gọi map.getDatasetFeatureLayer(), truyền mã nhận dạng tập dữ liệu.
  2. Áp dụng kiểu bằng cách đặt tuỳ chọn kiểu của tính năng (ví dụ: styleOptions) hoặc hàm (ví dụ: setStyle) trên lớp tập dữ liệu.

TypeScript

const datasetLayer = map.getDatasetFeatureLayer(datasetId);
datasetLayer.style = styleOptions;

JavaScript

const datasetLayer = map.getDatasetFeatureLayer(datasetId);

datasetLayer.style = styleOptions;

Xoá kiểu khỏi lớp

Để xoá kiểu khỏi một lớp, hãy đặt style thành null:

featureLayer.style = null;

Bạn cũng có thể trả về null từ hàm kiểu tính năng, chẳng hạn như nếu bạn muốn duy trì trạng thái ẩn của một tập hợp con các tính năng.

Thêm văn bản ghi công

Bản đồ của bạn phải hiển thị mọi thuộc tính bắt buộc khi hiển thị tập dữ liệu được tải lên trên Google Maps. Văn bản thuộc tính không được che khuất hoặc can thiệp vào biểu trưng Google.

Một cách để thêm văn bản thuộc tính là sử dụng chế độ điều khiển tuỳ chỉnh để đặt HTML tuỳ ý tại các vị trí tiêu chuẩn trên bản đồ. Mã ví dụ sau đây cho thấy một hàm có thể lập trình để tạo một thành phần điều khiển tuỳ chỉnh như vậy:

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

Sau khi xác định chế độ điều khiển, bạn có thể thêm chế độ điều khiển đó vào bản đồ tại thời điểm khởi chạy, như minh hoạ bên dưới:

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