Layers ใน LeafletJS คือชุดของข้อมูลทางภูมิศาสตร์ที่แสดงอยู่บนแผนที่ Layers สามารถประกอบด้วยข้อมูลประเภทต่างๆ เช่น แผนที่พื้นฐาน ข้อมูลภูมิศาสตร์ ไอคอน และเส้นทาง
มีสองประเภทหลักของ Layers ใน LeafletJS:
- Base layers: Base layers are mutually exclusive, meaning that only one base layer can be displayed on the map at a time. Base layers are typically used to display the background of the map, such as a street map or satellite imagery.
- Overlay layers: Overlay layers can be displayed on top of base layers. Overlay layers are typically used to display additional information on the map, such as points of interest, lines, or polygons.
To add a layer to a Leaflet map, you can use the map.addLayer()
function. For example, the following code adds a base layer to the map that displays a street map:
1 | var baseLayer = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { |
You can also add overlay layers to the map. For example, the following code adds an overlay layer to the map that displays a point of interest:
1 | var marker = L.marker([51.505, -0.09]); |
You can add multiple layers to the map. The order in which you add layers determines the order in which they are displayed. For example, if you add a base layer and then an overlay layer, the overlay layer will be displayed on top of the base layer.
You can also create layer groups to organize your layers. Layer groups are collections of layers that can be managed together. For example, you could create a layer group for all of your base layers and another layer group for all of your overlay layers. This would allow you to easily switch between base layers and overlay layers.
Layers are a powerful tool for displaying data on Leaflet maps. By using layers, you can create maps that are informative, engaging, and useful.
Here are some examples of how layers can be used in Leaflet maps:
- A map of the United States with a base layer showing a street map and overlay layers showing the boundaries of each state and the routes of major highways.
- A map of a city with a base layer showing a satellite image and overlay layers showing the locations of points of interest, such as restaurants, hotels, and parks.
- A map of a national park with a base layer showing a topographical map and overlay layers showing the locations of hiking trails, campgrounds, and ranger stations.
Layers are a versatile tool that can be used to create a wide variety of Leaflet maps. By learning how to use layers, you can create maps that are informative, engaging, and useful.