📅  最后修改于: 2023-12-03 15:17:18.211000             🧑  作者: Mango
LeafletJS是一个基于开放源代码GIS引擎的JavaScript库,它提供了各种功能,如地图显示,地图交互和数据可视化功能。LeafletJS-控件是LeafletJS库中的一个组件,它添加了一些常用的控件,如比例尺,缩放控制,方向指示器等。
LeafletJS-控件定义了以下控件类型:
要将LeafletJS-控件添加到您的项目中,请使用npm包管理器或手动下载和添加以下依赖关系:
npm install leaflet@1.7.1 leaflet-control@1.3.0
然后,使用以下JS和CSS文件将控件添加到您的地图中:
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" />
<link rel="stylesheet" href="https://unpkg.com/leaflet-control@1.3.0/dist/leaflet.control.css" />
<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script>
<script src="https://unpkg.com/leaflet-control@1.3.0/dist/leaflet.control.js"></script>
以下示例展示了如何在LeafletJS地图中添加比例尺控制器:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>LeafletJS Example</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" />
<link rel="stylesheet" href="https://unpkg.com/leaflet-control@1.3.0/dist/leaflet.control.css" />
<style>
#map {
width: 100%;
height: 500px;
}
</style>
</head>
<body>
<div id="map"></div>
<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script>
<script src="https://unpkg.com/leaflet-control@1.3.0/dist/leaflet.control.js"></script>
<script>
var map = L.map('map').setView([51.505, -0.09], 13);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: 'Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +
'<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
maxZoom: 18,
id: 'mapbox/streets-v11',
tileSize: 512,
zoomOffset: -1
}).addTo(map);
// add scale control to map
L.control.scale().addTo(map);
</script>
</body>
</html>
此示例创建了一个LeafletJS地图,并使用OpenStreetMap作为背景图层。然后,它使用L.control.scale()
将比例尺控制器添加到地图中。