1
|
L.Control.Ruler=L.Control.extend({options:{position:"topright",circleMarker:{color:"red",radius:2},lineStyle:{color:"red",dashArray:"1,6"},lengthUnit:{display:"km",decimal:2,factor:.001,label:"Distance:"},angleUnit:{display:"°",decimal:2,factor:360,label:"Bearing:"}},initialize(t){L.Util.setOptions(this,t),this._layers=new L.LayerGroup,this._enabled=!1},onAdd(t){this._defaultCursor=t._container.style.cursor,this._map=t;let e=L.DomUtil.create("div","leaflet-bar");return e.classList.add("leaflet-ruler"),L.DomEvent.disableClickPropagation(e),L.DomEvent.on(e,"click",this._toggleMeasure,this),this._container=e},onRemove(){L.DomEvent.off(this._container,"click",this._toggleMeasure,this)},_attachMouseEvents(){let t=this._map;t.doubleClickZoom.disable(),L.DomEvent.on(t._container,"keydown",this._escape,this),L.DomEvent.on(t._container,"dblclick",this._closePath,this),t._container.style.cursor="crosshair",t.on("click",this._addPoint,this),t.on(L.version.startsWith("2")?"pointermove":"mousemove",this._moving,this)},_removeMouseEvents(){let t=this._map;t.doubleClickZoom.enable(),L.DomEvent.off(t._container,"keydown",this._escape,this),L.DomEvent.off(t._container,"dblclick",this._closePath,this),t._container.style.cursor=this._defaultCursor,t.off("click",this._addPoint,this),t.off(L.version.startsWith("2")?"pointermove":"mousemove",this._moving,this)},_disable(){this._enabled=!1,this._container.classList.remove("leaflet-ruler-clicked"),this._layers.remove().clearLayers(),this._latlngs=[],this._totalLength=0,this._removeMouseEvents()},_enable(){this._enabled=!0,this._container.classList.add("leaflet-ruler-clicked"),this._circles=new L.FeatureGroup().addTo(this._layers),this._polyline=new L.Polyline([],this.options.lineStyle).addTo(this._layers),this._layers.addTo(this._map),this._latlngs=[],this._totalLength=0,this._attachMouseEvents()},_toggleMeasure(){this._enabled?this._disable():this._enable()},_drawTooltip(t,e,i){let s=this._latlngs[this._latlngs.length-1]??t,o=this._calculateBearing(s,t),l=s.distanceTo(t)*this.options.lengthUnit.factor,n=this._totalLength+l,a=n.toFixed(this.options.lengthUnit.decimal),h=i?'<br><div class="plus-length">(+'+l.toFixed(this.options.lengthUnit.decimal)+")</div>":"";this._totalLength=i?this._totalLength:n,e.getTooltip()||e.bindTooltip("",i?{direction:"auto",sticky:!0,offset:new L.Point(0,-40),className:"moving-tooltip"}:{permanent:!0,className:"result-tooltip"}).openTooltip(),e.setLatLng(t).setTooltipContent("<b>"+this.options.angleUnit.label+"</b> "+o.toFixed(this.options.angleUnit.decimal)+" "+this.options.angleUnit.display+"<br><b>"+this.options.lengthUnit.label+"</b> "+a+" "+this.options.lengthUnit.display+h)},_addPoint(t){let e=t.latlng||t,i=new L.CircleMarker(e,this.options.circleMarker).addTo(this._circles);this._polyline.addLatLng(e),this._latlngs.length&&!e.equals(this._latlngs[this._latlngs.length-1])&&this._drawTooltip(e,i,!1),this._latlngs.push(e)},_moving(t){if(this._latlngs.length){let e=this._latlngs[this._latlngs.length-1];this._tempLine||(this._tempLine=new L.Polyline([],this.options.lineStyle).addTo(this._map)),this._tempPoint||(this._tempPoint=new L.CircleMarker(t.latlng,this.options.circleMarker).addTo(this._map)),this._tempLine.setLatLngs([e,t.latlng]),this._drawTooltip(t.latlng,this._tempPoint,!0),L.DomEvent.off(this._container,"click",this._toggleMeasure,this)}},_escape(t){t.keyCode===27&&(this._latlngs.length?this._closePath():(this._enabled=!0,this._toggleMeasure()))},_calculateBearing(t,e){const i=Math.PI/180,s=this.options.angleUnit.factor/2/Math.PI;let o=Math.sin((e.lng-t.lng)*i)*Math.cos(e.lat*i),l=Math.cos(t.lat*i)*Math.sin(e.lat*i)-Math.sin(t.lat*i)*Math.cos(e.lat*i)*Math.cos((e.lng-t.lng)*i);return(Math.atan2(o,l)*s+this.options.angleUnit.factor)%this.options.angleUnit.factor},_closePath(){this._tempLine&&(this._tempLine.remove(),this._tempLine=null),this._tempPoint&&(this._tempPoint.remove(),this._tempLine=null),this._latlngs.length<=1&&this._circles.remove(),this._enabled=!1,L.DomEvent.on(this._container,"click",this._toggleMeasure,this),this._toggleMeasure()}}),L.control=L.control||{},L.control.ruler=function(t){return new L.Control.Ruler(t)};
|