leaflet.camera-view-marker.measure.js 11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
/*
  Leaflet.CameraViewMarker - specific marker with interactions like rotate and drag

  Copyright (C) 2017 Elphel Inc.

  License: GPLv3

  http://leafletjs.com
  https://www.elphel.com

*/
/** 
 * @file leaflet.camera-view-marker.measure.js
 * @brief extends Leaflet.CameraViewMarker with distance measuring tool
 * 
 * @copyright Copyright (C) 2017 Elphel Inc.
 * @author Oleg Dzhimiev <oleg@elphel.com>
 *
 * @licstart  The following is the entire license notice for the 
 * JavaScript code in this page.
 *
 *   The JavaScript code in this page is free software: you can
 *   redistribute it and/or modify it under the terms of the GNU
 *   General Public License (GNU GPL) as published by the Free Software
 *   Foundation, either version 3 of the License, or (at your option)
 *   any later version.  The code is distributed WITHOUT ANY WARRANTY;
 *   without even the implied warranty of MERCHANTABILITY or FITNESS
 *   FOR A PARTICULAR PURPOSE.  See the GNU GPL for more details.
 *
 *   As additional permission under GNU GPL version 3 section 7, you
 *   may distribute non-source (e.g., minimized or compacted) forms of
 *   that code without the copy of the GNU GPL normally required by
 *   section 4, provided you include this license notice and a URL
 *   through which recipients can access the Corresponding Source.
 *
 *  @licend  The above is the entire license notice
 *  for the JavaScript code in this page.
 */

// Reference plugins:
// * https://github.com/lvoogdt/Leaflet.awesome-markers
// * https://github.com/ppete2/Leaflet.PolylineMeasure

// * https://github.com/ewoken/Leaflet.MovingMarker/blob/master/MovingMarker.js
// * (not used) https://blog.webkid.io/rarely-used-leaflet-features/

(function (window, document, undefined) {
    "use strict";

    L.CameraViewMarker.include({

        createMeasureMarker: function(param,distance){
            
            var latlng = param;
            
            // param is event
            if(param.target){
                latlng = param.latlng;
            }
            
            var p1_ll = this._latlng;

            // param was angle then need distance
            if (!(latlng instanceof L.LatLng)){
                latlng = p1_ll.CoordinatesOf(param,distance);
            }
            
            var p2_ll = latlng;
            var l_d = Array(p1_ll,p2_ll);
            
            var pll = L.polyline(l_d, {
                    color: '#1f1', 
                    weight:1, 
                    dashArray:"5,5"
                }).addTo(this._layerPaint).bringToBack();
                
            //circle
            var tmp_point = new L.CircleMarker(latlng,{
                color: '#1f1',
                fillColor: '#0f3',
                weight: 2,
                fillOpacity: 0.5,
                radius: 5,
            }).addTo(this._layerPaint);
        
            var distance = latlng.distanceTo(this._latlng).toFixed(1);
        
            tmp_point.bindTooltip(distance+' m',{
                permanent:"true",
                direction:"right",
                className: "measurementtooltip",
                offset:[0,0],
            }).openTooltip();
        
            tmp_point.on('click',this._measureMarkerClick,this);
            tmp_point.on('mousedown',this._dragMeasureMarker,this);
            tmp_point._index = this._measureMarkers.length;
        
            this._measureMarkers.push(tmp_point);
            this._measureLines.push(pll);
            
            return tmp_point._index;

        },
        
        moveMeasureMarker: function(param,index){
            
            var latlng = param;
            
            if (param.target){
                index = this.draggedMarker._index;
                latlng = param.latlng;
113 114 115
                
                // prevent image getting grabbed by browser
                param.originalEvent.preventDefault();
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
            }
            
            var p1_ll = this._latlng;
            var p2_ll = latlng;
            var l_d = Array(p1_ll,p2_ll);
            
            this._measureMarkers[index].setLatLng(latlng);
            this._measureLines[index].setLatLngs(l_d);
                        
            var distance = p2_ll.distanceTo(p1_ll).toFixed(1);
            this._measureMarkers[index]._tooltip.setContent(distance+' m');
            
            this.draggedMarker = {
                _index: index,
                _latlng: latlng
            };
            
            this._syncMeasureMarkersToBasePoint();
            
        },
      
        removeMeasureMarker: function(param){
            
            var index = param;
            
            if(param.target){
                index = param.target._index;
                L.DomEvent.stopPropagation(param);
            }
            
            this._layerPaint.removeLayer(this._measureMarkers[index]);
            this._layerPaint.removeLayer(this._measureLines[index]);
            
            this._measureMarkers.splice(index,1);
            this._measureLines.splice(index,1);
            
            this._updateMeasureMarkersIndices();
            
        },

        placeSlidingMarker: function(angle,distance){
          
            var p1_ll = this._measureBase;
            var p2_ll = p1_ll.CoordinatesOf(angle,distance);
            
            var l_d = Array(p1_ll,p2_ll);

            if (this._slidingMarker == undefined){
                this._slidingMarker = new L.CircleMarker(p2_ll,{
                    color: '#1b1',
                    fillColor: '#0f3',
                    weight: 2,
                    fillOpacity: 0.5,
                    radius: 5,
                }).addTo(this._layerPaint);
                
                this._slidingLine = L.polyline(l_d, {
                    color: '#1f1', 
                    weight:1, 
                    dashArray:"5,5"
                }).addTo(this._layerPaint).bringToBack();
                
                this._slidingMarker.bindTooltip(distance.toFixed(1)+' m',{
                    permanent:"true",
                    direction:"right",
                    className: "measurementtooltip",
                    offset:[0,0],
                }).openTooltip();
                
            }else{
                
                this._slidingMarker.setLatLng(p2_ll);
                this._slidingLine.setLatLngs(l_d);
                this._slidingMarker._tooltip.setContent(distance.toFixed(1)+' m');

            }
            
        },
        
        removeSlidingMarker: function(){
            
            if (this._slidingMarker != undefined){
            
                this._layerPaint.removeLayer(this._slidingMarker);
                this._layerPaint.removeLayer(this._slidingLine);
                delete this._slidingMarker;
                delete this._slidingLine;
            
            }
            
        },
        
        onAdd: function(){

            this._initCameraViewMarker();
            this._initCVM_M();

        },

        _initCVM_M: function(){
            
            this._measuring = false;
            
            this._measureMarkers = Array();
            this._measureLines = Array();
            
            this._map.doubleClickZoom.disable();
            
            this._registerEvents_M();
            
            this._measureBase = this._latlng;
            
            this.draggedMarker = {
                _index: null,
                _latlng: null
            };
            //turn on measure mode
            //this._toggleMeasureMode();
        },

        _registerEvents_M: function(){

            this._map.on('mousemove',this._mouseMove_M,this);
            this._map.on('click', this._toggleMeasureMode, this);
            this._map.on('mousemove',this._syncMeasureMarkersToBasePoint, this);
            
        },

        _mouseMove_M: function(e){

            if (e.originalEvent.ctrlKey){
                this._map._container.style.cursor = "pointer";
            }else{
                this._map._container.style.cursor = "default";
            }

        },
        
        _toggleMeasureMode: function(e){
        
            if (e.originalEvent.ctrlKey){
                this.createMeasureMarker(e);
            }
        
            /*
            self._measuring = !self._measuring;
            
            if(self._measuring){
            self._basePoint.off('mousedown',self._dragCamera, self);
            self._map._container.style.cursor = "crosshair";
            
            self._map.on('click', self._placeMeasurePoint, self);
            
            }else{
            self._basePoint.on('mousedown',self._dragCamera, self);
            self._map._container.style.cursor = "default";
            self._map.off('click', self._placeMeasurePoint, self);
            }
            */
        },
      
        _measureMarkerClick:function(e){

            if (e.originalEvent.ctrlKey){
                this.removeMeasureMarker(e);
            }

        },
              
285
        _syncMeasureMarkersToBasePoint: function(e){
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357

            if (this._measureMarkers.length!=0){
                if (this._measureBase!=this._latlng){
                    
                    var self = this;
                    
                    this._measureMarkers.forEach(function(c,i){
                        
                        var p1_ll = self._latlng;
                        var p2_ll = c.getLatLng();
                        var l_d = Array(p1_ll,p2_ll);
                    
                        self._measureLines[i].setLatLngs(l_d);
                    
                        var distance = p2_ll.distanceTo(p1_ll).toFixed(1);
                        c._tooltip.setContent(distance+' m');
                    });

                    this._measureBase=this._latlng;
                }
            }
            
        },
      
        _dragMeasureMarker: function(e){

            if (!e.originalEvent.ctrlKey){
            
                this.draggedMarker = {
                    _index: e.target._index,
                    _latlng: e.target._latlng
                };
                
                this._map.dragging.disable();

                this._map.off('mousemove',this._mouseMove,this);
                this._map.off('click',this._mouseClick,this);
                
                this._map.on('mousemove',this.moveMeasureMarker,this);
                
                this._map.on ('mouseup',this._mouseUp_M,this);
            }
            
        },
      
        _mouseUp_M: function(){
            
            this._map.off('mousemove',this.moveMeasureMarker,this);
            
            this._map.dragging.enable();
            
            this._map.on ('mousemove',this._mouseMove,this);
            
            this._map.off ('mouseup',this._mouseUp_M,this);
            
            this.draggedMarker._index = null;
            
        },
      
        _updateMeasureMarkersIndices:function(){
            
            var self = this;
            
            this._measureMarkers.forEach(function(c,i){
                self._measureMarkers[i]._index = i;
            });

        },
      
    });
    
}(this,document));