leaflet官網:http://leafletjs.com/spa
效果:code
腳本:orm
var map = L.map('map', { center: [25.1026993454,102.9312515259], // 地圖中心點(昆明) zoom: 16, // 地圖縮放層級 zoomControl: false, // 縮放 doubleClickZoom: false, // 禁止雙擊放大 attributionControl: false // 版權 }); var kmgLayer = L.tileLayer.wms('wms切片圖層地址', { layers: 'airport:kmg', format: 'image/jpeg', transparent: false }); map.addLayer(kmgLayer); // 繪製區域 var layerObj = {}; function drawPolygon() { var points = [], points_length = 0, polyline, polygon; // 單擊 var clickFlag, clickTimes = 1, isDrag = false; map.on('mousedown', function(e) { map.off('mousemove'); if(clickFlag) clearTimeout(clickFlag); clickFlag = setTimeout(function() { if(clickTimes==1 && !isDrag) { points.push([e.latlng.lat, e.latlng.lng]); points_length = points.length; // 移動 map.on('mousemove', function(e) { // 清除 if(polyline) map.removeLayer(polyline); if(polygon) map.removeLayer(polygon); // polyline points[points_length] = [e.latlng.lat, e.latlng.lng]; polyline = new L.Polyline(points); map.addLayer(polyline); // polygon polygon = new L.Polygon(points); map.addLayer(polygon); }); } }, 300); }); // 雙擊 map.on('dblclick', function() { if(points.length) { clickTimes = 2; // polyline polyline = new L.Polyline(points); map.addLayer(polyline); // polygon polygon = new L.Polygon(points); map.addLayer(polygon); // 移除事件 map.off('mousemove'); setTimeout(function() { clickTimes = 1; // 保存layer(方便後面刪除) var layerName = prompt('請入名稱'); if(layerName) { layerObj[layerName] = [polyline, polygon]; console.log(layerObj); } points = []; }, 300); } }); // 鍵盤事件 $(document).keyup(function(e) { if(e.keyCode == 27) {// esc鍵 if(points.length) { map.off('mousemove'); clickTimes = 1; map.removeLayer(polyline); map.removeLayer(polygon); points = []; } } }); // 拖動 map.on('movestart', function() { isDrag = true; }); map.on('moveend', function() { isDrag = false; }); } drawPolygon();