L.Control.DrawCircles = L.Control.extend({ //是否初始化 _initialized:false, //是否完成当前测绘 _finished:true, curcircles:null, circlesdata:{ startpoint:null, stoppoint:null, radius:null, }, options:{ position:'topright', autoZIndex:true, offset:[10,40] }, initialize:function(options){ L.setOptions(this, options); return this; }, onAdd:function(map){ this._map = map; this._createControl(); switch(this.options.position){ case 'topleft': this._container.style.marginLeft = this.options.offset[0]+'px'; this._container.style.marginTop = this.options.offset[1]+'px'; break; case 'topright': this._container.style.marginRight = this.options.offset[0]+'px'; this._container.style.marginTop = this.options.offset[1]+'px'; break; case 'bottomleft': this._container.style.marginLeft = this.options.offset[0]+'px'; this._container.style.marginBottom = this.options.offset[1]+'px'; break; case 'bottomright': this._container.style.marginRight = this.options.offset[0]+'px'; this._container.style.marginBottom = this.options.offset[1]+'px'; break; } return this._container; }, _createControl:function(){ var _this = this; this._container = L.DomUtil.create('div','leaflet-bar leaflet-control-measure-drawcircles'); var link = L.DomUtil.create('a','leaflet-control-measure-drawcircles-link',this._container); link.title = '画圆'; var drawSpan = L.DomUtil.create('span','',link); drawSpan.innerHTML = "圆"; _this.start(); L.DomEvent .on(this._container,'contextmenu',L.DomEvent.stopPropagation) .on(link,'click', L.DomEvent.stopPropagation) .on(link,'click',function(){ _this.start(); }) }, start:function(){ var _this = this; L.DomUtil.addClass(_this._container,'active'); if(L.Browser.ie || L.Browser.firefox){ _this._map.getContainer().style.cursor = 'url('+L.DefaultImagePath+'/cur-ruler.cur),auto'; }else{ _this._map.getContainer().style.cursor = 'url('+L.DefaultImagePath+'/cur-ruler.cur) 5 5,auto'; } map2DViewer.map.off('measure-drawCircle-result', map2DViewer.drawCircleResult); map2DViewer.map.on('measure-drawCircle-result', map2DViewer.drawCircleResult); _this._map.dragging.disable(); _this._map.on("mousedown",_this.circleMousedown,this) .on("mouseup",_this.circleUp,this); }, circleUp:function(){ var _this = this; _this._map.dragging.enable(); _this._map.off("mousedown",_this.circleMousedown,this) .off("mousemove",_this.circleMove,this) .off("mouseup",_this.circleUp,this); _this._map.fire('measure-drawCircle-result',_this.circlesdata); _this._map.off('measure-drawCircle-result', map2DViewer.drawCircleResult); _this._map.removeLayer(_this.curcircles); map2DViewer.map.getContainer().style.cursor = 'auto'; }, stop:function(){ var _this = this; _this._map.dragging.enable(); _this._map.off("mousedown",_this.circleMousedown,this) .off("mousemove",_this.circleMove,this) .off("mouseup",_this.circleUp,this); _this._map.off('measure-drawCircle-result', map2DViewer.drawCircleResult); map2DViewer.map.getContainer().style.cursor = 'auto'; }, circleMousedown:function(e){ var _this = this; _this.circlesdata.startpoint = [e.latlng.lat,e.latlng.lng]; _this.curcircles = L.circle( _this.circlesdata.startpoint, 0, { color: '#ff0000', weight: 3, fillColor: '#ff6600', opacity: 0.7, fillOpacity: 0.2, } ).addTo(_this._map); _this._map.on("mousemove",_this.circleMove,this); }, circleMove:function(e){ var _this = this; _this.circlesdata.stoppoint = [e.latlng.lat,e.latlng.lng]; var distance = new L.LatLng(_this.circlesdata.startpoint[1],_this.circlesdata.startpoint[0]).distanceTo(new L.LatLng(_this.circlesdata.stoppoint[1],_this.circlesdata.stoppoint[0])); _this.curcircles.setRadius(distance); _this.circlesdata.radius = distance; } }); L.control.drawcircles = function(options){ return new L.Control.DrawCircles(options); } //2D画圆 map2DViewer.drawCircleResult = function(data) {}; map2DViewer.drawCircle = function(options){ this.DrawCircles = new L.Control.DrawCircles({ position: 'topleft', autoZIndex:true, offset:[150,50] }).addTo(this.map); } map2DViewer.removeCircle = function(){ if(this.DrawCircles){ this.DrawCircles.stop(); this.map.removeControl(this.DrawCircles); this.DrawCircles = false; } }