add
sbjw
2025-08-13 5411e296a44e26ba08ff9eec5ad9ae855ddf756a
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
import React, {PureComponent} from "react";
import {Button} from "antd";
 
export default class ProfileTool extends PureComponent {
 
    constructor(props) {
        super(props);
        this.layers = null;
        this.positions = [];
        this.handlerLine = undefined;
        this.mapApiService = this.props.mapApiService;
        this.viewer = this.props.mapApiService.viewer;
    }
 
    componentDidMount() {
        // 订阅事件
        if (!this.handlerLine) {
            this.handlerLine = this.mapApiService.getMeasureHandler();
            //注册测距功能事件
            this.handlerLine.measureEvt.addEventListener(function (result) {
                this.positions = result.positions;
                let dis = Number(result.distance);
                let distance = dis > 1000 ? (dis / 1000).toFixed(2) + 'km' : dis.toFixed(2) + 'm';
                this.handlerLine.disLabel.text = '距离:' + distance;
                if (this.positions.length > 2) {
                    this.positions = [this.positions[0], this.positions[1]];
                    this.handlerLine.deactivate();
                }
            }.bind(this));
            this.handlerLine.activeEvt.addEventListener(function (isActive) {
                if (isActive) {
                    this.viewer.enableCursorStyle = false;
                    this.viewer._element.style.cursor = '';
                } else {
                    this.viewer.enableCursorStyle = true;
                    if (this.positions.length == 2) {
                        this.doSampling();
                    }
                }
            }.bind(this));
        }
    }
 
    componentWillUnmount() {
 
    }
 
    closeEvent() {
        this.clear();
        this.props.callBack(null);
        this.props.closeEvent();
    }
 
    callBack(data) {
        this.props.callBack(data);
    }
 
    doSampling() {
        if (this.positions && this.positions.length == 2) {
            this.callBack(this.positions);
        }
    }
 
    draw() {
        this.positions = [];
        this.handlerLine.activate();
    }
 
 
    /*
     * 清除
     */
    clear() {
        this.handlerLineClear();
        this.callBack(null);
        this.viewer.enableCursorStyle = true;
    }
 
    handlerLineClear() {
        this.handlerLine && this.handlerLine.clear();
    }
 
 
    render() {
        return (
            <div className="param-container">
                <Button type="primary" id="addViewPoint" className="button black" onClick={() => {
                    this.draw();
                }}>绘制
                </Button>
                <Button type="primary" id="addTargetPoint" className="button black" onClick={() => {
                    this.clear();
                }}>清除
                </Button>
                <Button type="primary" id="clear" className="button black" onClick={() => {
                    this.closeEvent();
                }}>关闭
                </Button>
            </div>
        );
    }
}