leaflet 添加icon点位
时间: 2024-12-08 19:16:36 浏览: 35
Leaflet是一个轻量级的JavaScript库,用于创建交互式地图应用。在Leaflet中添加图标到地图点位通常涉及以下几个步骤:
引入依赖:首先需要引入Leaflet库和图标文件。例如,可以使用CDN链接:
<link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css" /> <script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
初始化地图:
var map = L.map('map').setView([51.505, -0.09], 13); // 设置中心点和缩放级别
创建图标:
var icon = L.icon({ iconUrl: 'path/to/your/icon.png', // 图标URL iconSize: [38, 95], // 图标的尺寸 iconAnchor: [22, 94], // 文字锚点相对于图标的位置 popupAnchor: [-3, -76] // 当显示提示框时,文字的位置相对图标 });
添加图标到地图:
var marker = L.marker([lat, lng], {icon: icon}).addTo(map); // 替换lat和lng为你想要放置标记的经纬度
可选地,给图标添加事件监听和自定义信息:
marker.bindPopup("This is a marker with custom info").openPopup();
相关推荐














