码迷,mamicode.com
首页 > 其他好文 > 详细

OpenLayers动态测量距离和面积,并可自定义测量的线样式

时间:2020-01-15 11:43:34      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:tle   nat   pst   信息   ble   end   fse   start   定义   

全局操作变量

/**
 *  @description 标注弹出框
 */
HtmlPopup = null;
/**
 *  @description 临时图层类数据源
 */
VectorSource = null;
/**
 *  @description 绘图工具
 */
Draw = null;

    let measureTooltipElement;
    let measureTooltip=HtmlPopup;
    let drawingFeature = null;

 

 

动态测量距离

 //动态测量距离
    this.dynamicLengthMeasure = function (_map,_measureLengthStyle,_measureTooltipStyle,_measureEndCallback) {
        measureTooltipElement = document.createElement("div");
        //暂停绘制
        if (Draw != null) {
            _map.removeInteraction(Draw);
            Draw = null;
        }
        if(_measureLengthStyle){
            Draw = new ol.interaction.Draw({
                //绘制层数据源
                source: VectorSource,
                /** @type {ol.geom.GeometryType}几何图形类型 */
                type: ‘LineString‘,
                //几何信息变更时调用函数
                //geometryFunction: geometryFunction,
                freehand: false,
                style:_measureLengthStyle
            });
        }else{
            Draw = new ol.interaction.Draw({
                //绘制层数据源
                source: VectorSource,
                /** @type {ol.geom.GeometryType}几何图形类型 */
                type: ‘LineString‘,
                //几何信息变更时调用函数
                //geometryFunction: geometryFunction,
                freehand: false,
            });
        }

        _map.addInteraction(Draw);
        Draw.on(‘drawstart‘, function (e) {
            if(_measureLengthStyle){
                e.feature.setStyle(_measureLengthStyle);
            }
            measureTooltip = createMeasureTooltip(measureTooltip,_measureTooltipStyle,_map);
            drawingFeature = e.feature;
            let tooltipCoord = e.coordinate;
            drawingFeature.getGeometry().on(‘change‘,function (evt) {
                let geom = evt.target;
                let output = geom.getLength();
                if (output > 100) {
                    output = (Math.round(output / 1000 * 100) / 100) + ‘ km‘;
                } else {
                    output = (Math.round(output * 100) / 100) + ‘ m‘;
                }
                tooltipCoord = geom.getLastCoordinate();
                measureTooltipElement.innerHTML = output;
                measureTooltip.setPosition(tooltipCoord);
            });
        });
        Draw.on(‘drawend‘, function (e) {
            _map.removeInteraction(Draw);
            Draw = null;
            if(_measureLengthStyle){
                e.feature.setStyle(_measureLengthStyle);
            }
            measureTooltip = createMeasureTooltip(measureTooltip,_measureTooltipStyle,_map);
            drawingFeature = e.feature;
            let output = e.feature.getGeometry().getLength();
            if (output > 100) {
                output = (Math.round(output / 1000 * 100) / 100) + ‘ km‘;
            } else {
                output = (Math.round(output * 100) / 100) + ‘ m‘;
            }
            let tooltipCoord = e.feature.getGeometry().getLastCoordinate();
            measureTooltipElement.innerHTML = output;
            measureTooltip.setPosition(tooltipCoord);

            if(_measureEndCallback){
                let measureEndCallbackkObj = {};
                measureEndCallbackkObj.feature=e.feature;
                measureEndCallbackkObj.length = output;
                _measureEndCallback(measureEndCallbackkObj);
            }
        });
        function createMeasureTooltip(measureTooltip,_param,_map) {
            if(_param==null||_param==undefined){
                _param={};
            }
            measureTooltipElement.className = _param.className||‘ol-overlay-container ol-selectable‘;
            measureTooltipElement.id=‘measureLength_DIV‘;
            measureTooltip = new ol.Overlay({
                element: measureTooltipElement,
                id:‘measureLength_DIV‘,
                offset: _param.offset||[0, -15],
                positioning: _param.positioning||‘bottom-center‘   //显示的位置
            });
            _map.addOverlay(measureTooltip);
            return measureTooltip;
        }
    };

 

 

技术图片

 

 

动态测量面积

//动态测量面积
    this.dynamicAreaMeasure = function (_map,_measureAreaStyle,_measureTooltipStyle,_measureEndCallback) {
        // if (measureTooltipElement) {
        //     measureTooltipElement.parentNode.removeChild(measureTooltipElement)
        // }
        measureTooltipElement = document.createElement("div");
        //暂停绘制
        if (Draw != null) {
            _map.removeInteraction(Draw);
            Draw = null;
        }
        if(_measureAreaStyle){
            Draw = new ol.interaction.Draw({
                //绘制层数据源
                source: VectorSource,
                /** @type {ol.geom.GeometryType}几何图形类型 */
                type: ‘Polygon‘,
                //几何信息变更时调用函数
                //geometryFunction: geometryFunction,
                freehand: false,
                style:_measureAreaStyle
            });
        }else{
            Draw = new ol.interaction.Draw({
                //绘制层数据源
                source: VectorSource,
                /** @type {ol.geom.GeometryType}几何图形类型 */
                type: ‘Polygon‘,
                //几何信息变更时调用函数
                //geometryFunction: geometryFunction,
                freehand: false,
            });
        }

        _map.addInteraction(Draw);
        Draw.on(‘drawstart‘, function (e) {
            if(_measureAreaStyle){
                e.feature.setStyle(_measureAreaStyle);
            }

            measureTooltip = createMeasureTooltip(measureTooltip,_measureTooltipStyle,_map);

            drawingFeature = e.feature;
            let tooltipCoord = e.coordinate;
            drawingFeature.getGeometry().on(‘change‘,function (evt) {
                let geom = evt.target;
                let output = geom.getArea();
                if (output > 10000) {
                    output = (Math.round(output / 1000000 * 100) / 100) + ‘ km<sup>2</sup>‘
                } else {
                    output = (Math.round(output * 100) / 100) + ‘ m<sup>2</sup>‘
                }
                tooltipCoord = geom.getLastCoordinate();
                measureTooltipElement.innerHTML = output;
                measureTooltip.setPosition(tooltipCoord);
            });
        });
        Draw.on(‘drawend‘, function (e) {
            _map.removeInteraction(Draw);
            Draw = null;
            if(_measureAreaStyle){
                e.feature.setStyle(_measureAreaStyle);
            }
            // drawingFeature = null;
            // measureTooltipElement = null;
            // measureTooltip = createMeasureTooltip(measureTooltip,_measureTooltipStyle,_map);
            // createMeasureTooltip(measureTooltip,_measureTooltipStyle,_map);
            measureTooltip = createMeasureTooltip(measureTooltip,_measureTooltipStyle,_map);
            drawingFeature = e.feature;
            // let tooltipCoord = e.coordinate;
            // let geom = e.target;
            let output = e.feature.getGeometry().getArea();
            if (output > 10000) {
                output = (Math.round(output / 1000000 * 100) / 100) + ‘ km<sup>2</sup>‘
            } else {
                output = (Math.round(output * 100) / 100) + ‘ m<sup>2</sup>‘
            }
            let tooltipCoord = e.feature.getGeometry().getLastCoordinate();
            measureTooltipElement.innerHTML = output;
            measureTooltip.setPosition(tooltipCoord);

            if(_measureEndCallback){
                let measureEndCallbackkObj = {};
                // measureEndCallbackkObj.attributes={};
                measureEndCallbackkObj.feature=e.feature;
                measureEndCallbackkObj.length = output;
                // measureEndCallbackkObj.attributes.x = (e.feature.getGeometry().getExtent()[0]+e.feature.getGeometry().getExtent()[2])/2;
                // measureEndCallbackkObj.attributes.y = (e.feature.getGeometry().getExtent()[1]+e.feature.getGeometry().getExtent()[3])/2;
                _measureEndCallback(measureEndCallbackkObj);
            }
        });
        function createMeasureTooltip(measureTooltip,_param,_map) {
            // if (measureTooltipElement) {
            //     measureTooltipElement.parentNode.removeChild(measureTooltipElement)
            // }
            // measureTooltipElement = document.createElement("div");
            // if(_param){
            if(_param==null||_param==undefined){
                _param={};
            }
            measureTooltipElement.className = _param.className||‘ol-overlay-container ol-selectable‘;
            measureTooltipElement.id=‘measureArea_DIV‘;
            measureTooltip = new ol.Overlay({
                id:‘measureArea_DIV‘,
                element: measureTooltipElement,
                offset: _param.offset||[0, -15],
                positioning: _param.positioning||‘bottom-center‘
            });
            // }
            _map.addOverlay(measureTooltip);
            return measureTooltip;
        }

技术图片

OpenLayers动态测量距离和面积,并可自定义测量的线样式

标签:tle   nat   pst   信息   ble   end   fse   start   定义   

原文地址:https://www.cnblogs.com/yangzhengier/p/12195534.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!