Leaflet双屏对比,地图联动


19年了 决定自己有时间开始写写博客,多积累些知识。
第一条博客,leaflet的双屏对比或者说地图联动。
话不多说直接上代码

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
    <title>leafletdemo</title>
	<link rel="stylesheet" href="leaflet1.34/leaflet.css">
	<script src="leaflet1.34/leaflet-src.js"></script>
	<style>
	body,html{height:100%;}
		#mapdiv1{
			width: 50%;
			height:100%;
			float: right;
		}
		#mapdiv2{
			float: left;
			width: 50%;
			height:100%;
		}
	</style>
</head>
<body>
	<div id="mapdiv1"></div>
    <div id="mapdiv2"></div>
	<script>
		var map1 = L.map("mapdiv1").setView([51.505, -0.09],13);
		var map2 = L.map('mapdiv2').setView([51.505, -0.09], 13); 
        var lay1 = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',{attribution:'&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'}
	       );
        var lay2 = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',{attribution:'&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'});
        lay1.addTo(map1);
        lay2.addTo(map2);
        var maps = [map1,map2]; 
	  //地图联动实现  
	  function maplink(e){  
	    var _this = this;  
	    maps.map(function (t) {  
	      t.setView(_this.getCenter(),_this.getZoom())  
	    })  
	  }  
	  //绑定  
	  maps.map(function (t) {  
	    t.on({drag:maplink,zoom:maplink})  
	  })

	</script>
</body>
</html>

转载自:https://blog.csdn.net/xu762102319/article/details/87614867

You may also like...