Lớp
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Lớp là các đối tượng trên bản đồ bao gồm một hoặc nhiều mục riêng biệt, nhưng được thao tác dưới dạng một đơn vị duy nhất. Các lớp thường phản ánh các tập hợp đối tượng mà bạn thêm vào đầu bản đồ để chỉ định một mối liên kết phổ biến. API JavaScript của Maps quản lý việc trình bày các đối tượng trong các lớp bằng cách kết xuất các mục cấu thành của các đối tượng đó thành một đối tượng (thường là lớp phủ thẻ thông tin) và hiển thị các đối tượng đó khi khung nhìn của bản đồ thay đổi. Các lớp cũng có thể thay đổi lớp trình bày của bản đồ, thay đổi một chút các ô cơ sở theo cách nhất quán với lớp. Xin lưu ý rằng theo thiết kế, hầu hết các lớp có thể không được truy cập thông qua các đối tượng riêng lẻ, mà chỉ có thể được thao tác dưới dạng một đơn vị.
Tổng quan về lớp
Maps JavaScript API có một số loại lớp:
- Lớp dữ liệu Google Maps cung cấp một vùng chứa cho dữ liệu không gian địa lý tuỳ ý.
Bạn có thể sử dụng Lớp dữ liệu để lưu trữ dữ liệu tuỳ chỉnh hoặc để hiển thị dữ liệu GeoJSON trên bản đồ của Google.
-
Lớp bản đồ nhiệt hiển thị dữ liệu địa lý bằng hình ảnh trực quan Bản đồ nhiệt.
-
Lớp KML hiển thị các phần tử KML và GeoRSS thành lớp phủ thẻ thông tin Maps JavaScript API.
-
Lớp giao thông hiển thị tình trạng giao thông trên bản đồ.
-
Lớp phương tiện công cộng hiển thị mạng lưới phương tiện công cộng
của thành phố trên bản đồ.
- Đối tượng
Lớp đi xe đạp hiển thị một lớp đường dành cho xe đạp
và/hoặc lớp phủ dành riêng cho xe đạp thành một lớp chung. Theo mặc định, lớp này được trả về trong DirectionsRenderer khi yêu cầu chỉ đường theo phương thức di chuyển ĐẠP XE.
Thêm lớp
Để thêm một lớp vào bản đồ, bạn chỉ cần gọi setMap()
, truyền vào đó đối tượng bản đồ để hiển thị lớp. Tương tự, để ẩn một lớp, hãy gọi setMap()
, truyền null
.
Đoạn mã bên dưới đặt bản đồ ở giữa London, Vương quốc Anh và thêm Lớp giao thông công cộng.
var mapOptions = {
zoom: 13,
center: new google.maps.LatLng(51.5,-0.11)
}
var map = new google.maps.Map(document.getElementById("map"), mapOptions);
var transitLayer = new google.maps.TransitLayer();
transitLayer.setMap(map);
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-08-06 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-06 UTC."],[[["\u003cp\u003eLayers in Maps JavaScript API are objects consisting of multiple items that are treated as a single unit and often represent collections of objects on the map.\u003c/p\u003e\n"],["\u003cp\u003eThe API provides various layer types, including Data, Heatmap, KML, Traffic, Transit, and Bicycling layers, each serving a distinct purpose for displaying geospatial data.\u003c/p\u003e\n"],["\u003cp\u003eYou can easily add or hide a layer by calling the \u003ccode\u003esetMap()\u003c/code\u003e method, providing the map object or \u003ccode\u003enull\u003c/code\u003e respectively.\u003c/p\u003e\n"],["\u003cp\u003eThe example code demonstrates adding a Transit layer to a map centered on London.\u003c/p\u003e\n"]]],[],null,["Select platform: [Android](/maps/documentation/android-sdk/utility/multilayer \"View this page for the Android platform docs.\") [JavaScript](/maps/documentation/javascript/layers \"View this page for the JavaScript platform docs.\")\n\nLayers are objects on the map that consist of one or more separate items,\nbut are manipulated as a single unit. Layers generally reflect collections of\nobjects that you add on top of the map to designate a common association. The\nMaps JavaScript API manages the presentation of objects within layers\nby rendering their constituent items into one object (typically a tile overlay) and\ndisplaying them as the map's viewport changes. Layers may also alter the presentation\nlayer of the map itself, slightly altering the base tiles in a fashion\nconsistent with the layer. Note that most layers, by design, may not be\naccessed via their individual objects, but may only be manipulated as a\nunit.\n\nLayers Overview\n\nThe Maps JavaScript API has several types of layers:\n\n- The [**Google Maps Data\n layer**](/maps/documentation/javascript/datalayer) provides a container for arbitrary geospatial data. You can use the Data layer to store your custom data, or to display GeoJSON data on a Google map.\n- The [**Heatmap layer**](/maps/documentation/javascript/heatmaplayer) renders geographic data using a *Heatmap* visualization.\n- The [**KML layer**](/maps/documentation/javascript/kmllayer) renders KML and GeoRSS elements into a Maps JavaScript API tile overlay.\n- The [**Traffic layer**](/maps/documentation/javascript/trafficlayer#traffic_layer) displays traffic conditions on the map.\n- The [**Transit layer**](/maps/documentation/javascript/trafficlayer#transit_layer) displays the public transport network of your city on the map.\n- The [**Bicycling layer**](/maps/documentation/javascript/trafficlayer#bicycling_layer) object renders a layer of bike paths and/or bicycle-specific overlays into a common layer. This layer is returned by default within the **DirectionsRenderer** when requesting directions of travel mode **BICYCLING**.\n\nAdd a layer\n\nTo add a layer to a map, you only need to call `setMap()`,\npassing it the map object on which to display the layer. Similarly, to hide a\nlayer, call `setMap()`, passing `null`.\n\nThe below snippet centers the map on London, UK, and adds the [Transit\nlayer](/maps/documentation/javascript/trafficlayer#transit_layer). \n\n```javascript\nvar mapOptions = {\n zoom: 13,\n center: new google.maps.LatLng(51.5,-0.11)\n}\n\nvar map = new google.maps.Map(document.getElementById(\"map\"), mapOptions);\n\nvar transitLayer = new google.maps.TransitLayer();\ntransitLayer.setMap(map);\n```"]]