在地圖上進行一些文字的標注有些場景我們會用到,在這里我們文字標注用到了DivICon圖標,通過與mark相結合,將mark的圖標設置為DivICon圖標,進行文字標注,放張圖看看效果:
一、全部代碼
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>文字標注</title>
<link href="Script/leaflet/leaflet.css" rel="stylesheet" />
<style>
#map
{
width:1000px;
height:1000px;
}
.my-div-icon{
font-size:15px;
/*background:red;*/
/*width:5px;*/
color:red;
}
</style>
<script src="Script/leaflet/leaflet.js"></script>
</head>
<body>
<div id="map"></div>
<script type="text/javascript">
var map = new L.Map('map');
var osmUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
var osm = new L.TileLayer(osmUrl, { minZoom: 1, maxZoom: 18 });
map.addLayer(osm);
map.setView(new L.LatLng(31.864942016, 117.2882028929), 11);
var myIcon = L.divIcon({
html: "狗子",
className: 'my-div-icon',
iconSize:30
});
L.marker([31.864942016,117.2882028929], { icon: myIcon }).addTo(map);
</script>
</body>
</html>
二、總結
html是標注的內容,iconSize是圖標大小是個正方形概念,className這是div設置類名,進行css樣式設置所用,leaflet的圖標本質上就是div容器,所以會有這個屬性,關於樣式的設置,幾乎所有的文字css樣式都能設置看你需要。地圖是加載的OSM