czt
2 天以前 51faf3e9c3c613e7fb12db6c88356946f2429e0c
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
var layer;
 
$(document).ready(function () {
    layui.use(['layer'], function () {
        layer = layui.layer;
    });
});
function initMap() {
    // 可删除代码开始
    // 检查百度地图API是否加载成功
    if (typeof BMapGL === 'undefined') {
        layer.msg("地图加载失败,请检查网络连接!", {icon: 2});
        return;
    }
 
    // 检查容器是否存在
    var mapContainer = document.getElementById("map");
    if (!mapContainer) {
        layer.msg("地图容器未找到!", {icon: 2});
        return;
    }
 
    // 检查数据
    if (!patrolRecordList || patrolRecordList.length === 0) {
        layer.msg("暂无轨迹数据!", {icon: 2});
        var defaultPoint = new BMapGL.Point(116.404, 39.915);
        map.centerAndZoom(defaultPoint, 12);
        map.enableScrollWheelZoom(true);
        return;
    }
    //可删除代码结束
 
    // ========== 百度地图初始化与轨迹渲染 ==========
    // 1. 初始化地图
    var map = new BMapGL.Map("map");
    // 2. 获取轨迹点数据
    if (patrolRecordList.length === 0) {
        layer.msg("暂无轨迹数据!", {icon: 2});
        var defaultPoint = new BMapGL.Point(116.404, 39.915);
        map.centerAndZoom(defaultPoint, 14);
        map.enableScrollWheelZoom(true);
        return;
    }
 
 
    // 3. 构造轨迹点数组和标记点数组
    var points = [];       // 轨迹折线点
    var markers = [];      // 地图标记点
    var cardElements = document.querySelectorAll('.track-card'); // 所有卡片元素
 
 
 
    patrolRecordList.forEach(function (record, index) {
        var lng = record.longitude;
        var lat = record.latitude;
        var point = new BMapGL.Point(lng, lat);
        points.push(point);
 
        // 创建地图标记点
        var marker = new BMapGL.Marker(point);
        markers.push(marker);
 
        // 标记点弹窗信息
        var infoWindow = new BMapGL.InfoWindow(`
                <div style="font-size: 12px; line-height: 1.8;">
                    <p><strong>点位名称:</strong>${record.pointName}</p>
                    <p><strong>巡检人:</strong>${record.createBy}</p>
                    <p><strong>巡检时间:</strong>${new Date(record.createTime).toLocaleString()}</p>
                    <p><strong>经纬度:</strong>${lat}, ${lng}</p>
                    <p><strong>轨迹点ID:</strong>${record.id}</p>
                </div>
            `);
 
        // 标记点点击事件:高亮对应卡片 + 显示弹窗
        marker.addEventListener("click", function () {
            // 移除所有卡片的active样式
            cardElements.forEach(el => el.classList.remove('active'));
            // 高亮当前卡片
            cardElements[index].classList.add('active');
            // 滚动到当前卡片
            cardElements[index].scrollIntoView({behavior: 'smooth', block: 'center'});
            // 显示弹窗
            this.openInfoWindow(infoWindow);
        });
 
        // 卡片点击事件:定位到对应标记点 + 高亮卡片
        cardElements[index].addEventListener("click", function () {
            // 移除所有卡片的active样式
            cardElements.forEach(el => el.classList.remove('active'));
            // 高亮当前卡片
            this.classList.add('active');
            // 地图中心定位到当前标记点
            map.centerAndZoom(point, 15);
            // 显示标记点弹窗
            marker.openInfoWindow(infoWindow);
            // 轻微动画效果
            marker.setAnimation(BMAP_ANIMATION_BOUNCE);
            setTimeout(() => marker.setAnimation(null), 1500);
        });
 
        map.addOverlay(marker);
    });
 
    // 寻找points中间的点位
    var midIndex = Math.floor(points.length / 2);
    const centerPoint = points[midIndex] || new BMapGL.Point(116.404, 39.915);
    map.centerAndZoom(centerPoint, 13); // 14为地图缩放级别
 
    // 4. 绘制轨迹折线
    var polyline = new BMapGL.Polyline(points, {
        strokeColor: "#1890ff", // 主色调:蓝色
        strokeWeight: 6,        // 折线宽度
        strokeOpacity: 0.8,     // 透明度
        strokeStyle: 'solid'    // 实线
    });
    map.addOverlay(polyline);
 
    // // 5. 起点/终点特殊标记
    // // 起点标记
    // var startMarker = new BMapGL.Marker(points[0], {
    //     icon: new BMapGL.Icon("http://api.map.baidu.com/img/markers.png", new BMapGL.Size(25, 34), {
    //         offset: new BMapGL.Size(12, 34),
    //         imageOffset: new BMapGL.Size(0, 0) // 起点图标
    //     })
    // });
    // map.addOverlay(startMarker);
    // // startMarker.setLabel(new BMapGL.Label("起点:" + patrolRecordList[0].pointName, {
    // //     offset: new BMapGL.Size(30, -15),
    // //     styles: {fontSize: '12px', color: '#fff', background: '#52c41a', padding: '0px 14px', borderRadius: '3px'}
    // // }));
    // startMarker.setLabel(new BMapGL.Label("起点:" + patrolRecordList[0].pointName, {
    //     offset: new BMapGL.Size(30, -15),
    //     styles: {
    //         fontSize: '12px',
    //         color: '#fff',
    //         background: '#52c41a',
    //         padding: '8px 8px',  // 左右padding保持一致(14px),保证背景两侧空间对称
    //         borderRadius: '3px',
    //         textAlign: 'center'  // 新增:强制文字在背景内水平居中
    //     }
    // }));
    //
    // // 终点标记
    // var endMarker = new BMapGL.Marker(points[points.length - 1], {
    //     icon: new BMapGL.Icon("http://api.map.baidu.com/img/markers.png", new BMapGL.Size(25, 34), {
    //         offset: new BMapGL.Size(12, 34),
    //         imageOffset: new BMapGL.Size(-114, 0) // 终点图标
    //     })
    // });
    // map.addOverlay(endMarker);
    // endMarker.setLabel(new BMapGL.Label("终点:" + patrolRecordList[patrolRecordList.length - 1].pointName, {
    //     offset: new BMapGL.Size(30, -15),
    //     styles: {fontSize: '12px', color: '#fff', background: '#ff4d4f', padding: '0px 14px', borderRadius: '3px'}
    // }));
 
    // 6. 地图自适应所有轨迹点
    map.enableScrollWheelZoom(true); // 开启滚轮缩放
    map.setViewport(points); // 适配所有轨迹点
 
    // 7. 初始提示
    layer.msg("轨迹加载完成!点击卡片可定位到对应点位", {
        icon: 1,
        time: 3000,
        offset: ['20px', '20px']
    });
}
 
// 页面加载完成后初始化地图
window.onload = initMap;