Measure.js 8.64 KB
Newer Older
qzhxx's avatar
qzhxx committed
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 113 114 115 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 285 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 358 359 360 361 362 363 364 365 366 367 368
/**
 * Created by cp on 2018/8/20.
 */

/**
 * Created by cp on 2018/7/2.
 */


import Map from 'ol/Map.js';
import {unByKey} from 'ol/Observable.js';
import Overlay from 'ol/Overlay.js';
import {getArea, getLength} from 'ol/sphere.js';
import {LineString, Polygon} from 'ol/geom.js';
import Draw from 'ol/interaction/Draw.js';
import { Vector as VectorLayer} from 'ol/layer.js';
import { Vector as VectorSource} from 'ol/source.js';
import {Circle as CircleStyle, Fill, Stroke, Style} from 'ol/style.js';

// 定义类
class Measure {
  constructor(vueThis, mainMap) {
    this.vm = vueThis;//类中变量

    this.name='measure';
    if (mainMap) {
      this.map = mainMap.getMap();//地图
      this.mainMap = mainMap;
    }
    this.init();
  }

  /****初始化方法****/
  init(){
    let source = new VectorSource();
    let vector = new VectorLayer({
      source: source,
      style: new Style({
        fill: new Fill({
          color: 'rgba(255, 255, 255, 0.2)'
        }),
        stroke: new Stroke({
          color: '#ffcc33',
          width: 2
        }),
        image: new CircleStyle({
          radius: 7,
          fill: new Fill({
            color: '#ffcc33'
          })
        })
      })
    });

    vector.set("name",this.name);
    this.source=source;
    this.vector=vector;


    this.startMeasureStatus=false;//是否在绘制 初始化时候并不在绘制
    this.map.addLayer(vector);
    /**
     * Currently drawn feature.
     * @type {module:ol/Feature~Feature}
     */
    this.sketch=null;
    /**
     * The help tooltip element.
     * @type {Element}
     */
    this.helpTooltipElement=null;


    /**
     * Overlay to show the help messages.
     * @type {module:ol/Overlay}
     */
    this.helpTooltip=null;


    /**
     * The measure tooltip element.
     * @type {Element}
     */
    this.measureTooltipElement=null;


    /**
     * Overlay to show the measurement.
     * @type {module:ol/Overlay}
     */
    this.measureTooltip=null;


    /**
     * Message to show when the user is drawing a polygon.
     * @type {string}
     */
    this.continuePolygonMsg = '双击结束绘制';


    /**
     * Message to show when the user is drawing a line.
     * @type {string}
     */
    this.continueLineMsg = '双击结束绘制';

    //初始化样式
    this.initStyle();
  }



  /**
   * Handle pointer move.
   * @param {module:ol/MapBrowserEvent~MapBrowserEvent} evt The event.
   */
  pointerMoveHandler(evt) {
    if (evt.dragging) {
      return;
    }
    /** @type {string} */
    var helpMsg = '开始绘制';

    if (this.sketch) {
      var geom = (this.sketch.getGeometry());
      if (geom instanceof Polygon) {
        helpMsg = this.continuePolygonMsg;
      } else if (geom instanceof LineString) {
        helpMsg = this.continueLineMsg;
      }
    }
    this.helpTooltipElement.innerHTML = helpMsg;
    this.helpTooltip.setPosition(evt.coordinate);
    this.helpTooltipElement.classList.remove('hidden');
  };




  /****添加地图监听事件****/
  addMapEvent(){
    let classThis=this;
    let mapPointermove=classThis.map.on('pointermove', function(evt){
      classThis.pointerMoveHandler(evt);
    });
    let mapMouseout =classThis.map.getViewport().addEventListener('mouseout', function() {
      classThis.helpTooltipElement.classList.add('hidden');
    });
    this.mapPointermove=mapPointermove;
    this.mapMouseout=mapMouseout;

  }

  removeMapLisenter(){
    if(this.mapPointermove||this.mapMouseout){
      unByKey(this.mapPointermove);
      unByKey(this.mapMouseout);
      this.mapPointermove=null;
      this.mapMouseout=null;
      this.helpTooltipElement.innerHTML = "请选择绘制的点";
      this.helpTooltipElement.className = 'tooltip hidden';

    }
    this.startMeasureStatus=false;
  }


  /**
   * Format length output.
   * @param {module:ol/geom/LineString~LineString} line The line.
   * @return {string} The formatted length.
   */
  formatLength(line) {
    var length = getLength(line);
    var output;
    if (length > 100) {
      output = (Math.round(length / 1000 * 100) / 100) +
        ' ' + 'km';
    } else {
      output = (Math.round(length * 100) / 100) +
        ' ' + 'm';
    }
    return output;
  };


  /**
   * Format area output.
   * @param {module:ol/geom/Polygon~Polygon} polygon The polygon.
   * @return {string} Formatted area.
   */
  formatArea(polygon) {
    var area =getArea(polygon);
    var output;
    if (area > 10000) {
      output = (Math.round(area / 1000000 * 100) / 100) +
        ' ' + 'km<sup>2</sup>';
    } else {
      output = (Math.round(area * 100) / 100) +
        ' ' + 'm<sup>2</sup>';
    }
    return output;
  };


  initStyle(){
    let style = new Style({
      fill: new Fill({
        color: 'rgba(255, 255, 255, 0.5)'
      }),
      stroke: new Stroke({
        color: 'rgba(0, 0, 0, 0.5)',
        lineDash: [10, 10],
        width: 2
      }),
      image: new CircleStyle({
        radius: 5,
        stroke: new Stroke({
          color: 'rgba(0, 0, 0, 0.7)'
        }),
        fill: new Fill({
          color: 'rgba(255, 255, 255, 0.2)'
        })
      })
    });
    this.style=style;
  }


  /***  绘制的方法  类型有两种
   * 'Polygon'
   * 'LineString'
   * ****/
  addInteraction(type){

    let classThis=this;
    let draw = new Draw({
      source: classThis.source,
      type: type,
      style:classThis.style
    });
    classThis.map.addInteraction(draw);

    classThis.createMeasureTooltip();
    classThis.createHelpTooltip();

    let  listener;
    draw.on('drawstart',
      function(evt) {
        // set sketch
        classThis.sketch = evt.feature;
        /** @type {module:ol/coordinate~Coordinate|undefined} */
        var tooltipCoord = evt.coordinate;
        listener = classThis.sketch.getGeometry().on('change', function(evt) {
          var geom = evt.target;
          var output;
          if (geom instanceof Polygon) {
            output = classThis.formatArea(geom);
            tooltipCoord = geom.getInteriorPoint().getCoordinates();
          } else if (geom instanceof LineString) {
            output = classThis.formatLength(geom);
            tooltipCoord = geom.getLastCoordinate();
          }
          classThis.measureTooltipElement.innerHTML = output;
          classThis.measureTooltip.setPosition(tooltipCoord);
        });
      });

    draw.on('drawend',
      function() {
        classThis.measureTooltipElement.className = 'tooltip tooltip-static';
        classThis.measureTooltip.setOffset([0, -7]);
        // unset sketch
        classThis.sketch = null;
        // unset tooltip so that a new one can be created
        classThis.measureTooltipElement = null;
        unByKey(listener);
        classThis.endDraw();
        classThis.removeMapLisenter();

      });

    this.draw=draw;
  }



  /**
   * Creates a new help tooltip
   */
  createHelpTooltip() {
    if (this.helpTooltipElement) {
      this.helpTooltipElement.parentNode.removeChild(this.helpTooltipElement);
    }
    this.helpTooltipElement = document.createElement('div');
    this.helpTooltipElement.className = 'tooltip hidden';
    this.helpTooltip = new Overlay({
      element: this.helpTooltipElement,
      offset: [15, 0],
      positioning: 'center-left'
    });
    this.map.addOverlay(this.helpTooltip);
  }


  /**
   * Creates a new measure tooltip
   */
  createMeasureTooltip() {
    let classThis=this;

    if (classThis.measureTooltipElement) {
      classThis.measureTooltipElement.parentNode.removeChild(classThis.measureTooltipElement);
    }
    classThis.measureTooltipElement = document.createElement('div');
    classThis.measureTooltipElement.className = 'tooltip tooltip-measure';
    classThis.measureTooltip = new Overlay({
      element: classThis.measureTooltipElement,
      offset: [0, -15],
      positioning: 'bottom-center'
    });
    classThis.map.addOverlay(classThis.measureTooltip);
  }


  cleanMeasure(){
    this.endDraw();
    this.removeMapLisenter();
    this.map.removeOverlay(this.helpTooltip);
    this.map.removeOverlay(this.measureTooltip);
    if(this.measureTooltipElement){
      this.measureTooltipElement.innerHTML = "";
    }
    if(this.helpTooltipElement){
      this.helpTooltipElement.innerHTML = "";
    }
    this.clanLayer();
  }
  clanLayer(){
    this.vector.getSource().clear();
  }
  /****结束的接口***/
  endDraw(){
    let classThis=this;
    if(classThis.draw){
      this.map.removeInteraction(classThis.draw);
      classThis.draw=null;
    }
  }

  /****测量开始的接口***/
  startMeasure(type){
    if(!this.startMeasureStatus){
      this.addMapEvent();
      this.startMeasureStatus=true;
    }
    this.endDraw();
    this.addInteraction(type);

  }


}

// Measure.para = 'Allen'; 静态变量

export {Measure};