@ohuoy/easymap 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,318 @@
1
+
2
+ import {hasProperty,transform} from '../../utils/util'
3
+ import mapboxgl from 'mapbox-gl';
4
+ export default class ScanLayer{
5
+ drawArray = []
6
+ defaultDrawInfo = {
7
+ id:'',
8
+ colors:[],
9
+ coords:[],
10
+ refreshTime:null
11
+ }
12
+ constructor(id,_scanInfo) {
13
+ this.id = id;
14
+ this.type = 'custom';
15
+ this.renderingMode = '2d';
16
+ }
17
+
18
+ onAdd(map, gl) {
19
+ // create GLSL source for vertex shader
20
+ const vertexSource = `
21
+ uniform mat4 u_matrix;
22
+ attribute vec4 a_pos;
23
+ attribute vec4 a_color;
24
+ varying vec4 v_color;
25
+ void main() {
26
+ gl_Position = u_matrix * vec4(a_pos);
27
+ v_color = a_color;
28
+ }`;
29
+
30
+ // create GLSL source for fragment shader
31
+ const fragmentSource = `
32
+ precision mediump float;
33
+ varying vec4 v_color;
34
+ void main() {
35
+ gl_FragColor = v_color;
36
+ }`;
37
+
38
+ // create a vertex shader
39
+ const vertexShader = gl.createShader(gl.VERTEX_SHADER);
40
+ gl.shaderSource(vertexShader, vertexSource);
41
+ gl.compileShader(vertexShader);
42
+
43
+ // create a fragment shader
44
+ const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
45
+ gl.shaderSource(fragmentShader, fragmentSource);
46
+ gl.compileShader(fragmentShader);
47
+
48
+ // link the two shaders into a WebGL program
49
+ this.program = gl.createProgram();
50
+ gl.attachShader(this.program, vertexShader);
51
+ gl.attachShader(this.program, fragmentShader);
52
+ gl.linkProgram(this.program);
53
+
54
+ this.aPos = gl.getAttribLocation(this.program, 'a_pos');
55
+ this.gl = gl;
56
+ this.map = map;
57
+ // define vertices of the triangle to be rendered in the custom style layer
58
+
59
+ // create and initialize a WebGLBuffer to store vertex and color data
60
+ // this.buffer = gl.createBuffer();
61
+ // gl.bindBuffer(gl.ARRAY_BUFFER, this.buffer);
62
+ // gl.bufferData(
63
+ // gl.ARRAY_BUFFER,
64
+ // new Float32Array(),
65
+ // gl.STATIC_DRAW
66
+ // );
67
+ }
68
+
69
+ // method fired on each animation frame
70
+ // https://docs.mapbox.com/mapbox-gl-js/api/#map.event:render
71
+ render(gl, matrix) {
72
+ gl.useProgram(this.program);
73
+ gl.uniformMatrix4fv(
74
+ gl.getUniformLocation(this.program, 'u_matrix'),
75
+ false,
76
+ matrix
77
+ );
78
+ //gl.bindBuffer(gl.ARRAY_BUFFER, this.buffer);
79
+ // gl.enableVertexAttribArray(this.aPos);
80
+ // gl.vertexAttribPointer(this.aPos, 3, gl.FLOAT, false, 0, 0);
81
+ // gl.enable(gl.BLEND);
82
+ // gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
83
+ //gl.drawArrays(gl.TRIANGLE_STRIP, 0, 12);
84
+ this.draw()
85
+ }
86
+
87
+ //添加object 支持多路径添加
88
+ //data 二维数组 croods 路径列表 colors 二维数组
89
+ drawList(_scanInfo){
90
+ let props = ['id','coords','colors']
91
+ if(_scanInfo instanceof Array){
92
+ for(let _info of _scanInfo){
93
+ hasProperty(_info,props)
94
+ let currIndex = this.drawArray.findIndex(a=>a.id == _info.id)
95
+ _info.coords = _info.coords.map(a=>transform(a))
96
+ if(currIndex <0){
97
+ let entity = Object.assign({},this.defaultDrawInfo,_info)
98
+ entity.refreshTime = new Date();
99
+ this.drawArray.push(entity)
100
+ }else{
101
+ let entity = Object.assign({},this.defaultDrawInfo,_info)
102
+ entity.refreshTime = new Date();
103
+ this.drawArray[currIndex] = entity
104
+ }
105
+ }
106
+ }else{
107
+ hasProperty(_scanInfo,props)
108
+ _scanInfo.coords = _scanInfo.coords.map(a=>transform(a))
109
+ let currIndex = this.drawArray.findIndex(a=>a.id == _scanInfo.id)
110
+ if(currIndex <0){
111
+ let entity = Object.assign({},this.defaultDrawInfo,_scanInfo)
112
+ entity.refreshTime = new Date();
113
+ this.drawArray.push(entity)
114
+ }else{
115
+ let entity = Object.assign({},this.defaultDrawInfo,_scanInfo)
116
+ entity.refreshTime = new Date();
117
+ this.drawArray[currIndex] = entity
118
+ }
119
+ }
120
+ }
121
+
122
+
123
+ draw(){
124
+ for(let i in this.drawArray) {
125
+ if(this.drawArray[i].id){
126
+ this.drawArrayItem(this.drawArray[i])
127
+ }
128
+ }
129
+ }
130
+
131
+ //
132
+ createBufferData(item){
133
+ let height = 0.0000001
134
+ let arrs = []
135
+ //let colors = []
136
+ for(let cr in item.coords){
137
+ let _next = 1
138
+ if(cr !=0 ){
139
+ let p1 = mapboxgl.MercatorCoordinate.fromLngLat({
140
+ lng: item.coords[cr-_next][0],
141
+ lat: item.coords[cr-_next][1]
142
+ }, 10)
143
+ let p2 = mapboxgl.MercatorCoordinate.fromLngLat({
144
+ lng: item.coords[cr][0],
145
+ lat: item.coords[cr][1]
146
+ }, 10)
147
+ if(p1.x == p2.x && p1.y == p2.y && p1.z== p2.z){
148
+ _next +=1
149
+ }else{
150
+ //创建矩形区域
151
+ for(let _c in item.colors[cr]){
152
+ let _height = height + height *Number(_c)
153
+ //创建垂直矩形区域
154
+ // let rect = [
155
+ // p1.x,p1.y,p1.z,
156
+ // p2.x,p2.y,p2.z,
157
+ // p1.x,p1.y,_height,
158
+ // p2.x,p2.y,_height,
159
+ // ]
160
+ let _colorinfo = item.colors[cr][_c]
161
+ let _color = [_colorinfo[0]/255,_colorinfo[1]/255,_colorinfo[2]/255,_colorinfo[3]]
162
+ arrs.push(p1.x)
163
+ arrs.push(p1.y)
164
+ arrs.push(p1.z)
165
+ arrs.push(_color[0])
166
+ arrs.push(_color[1])
167
+ arrs.push(_color[2])
168
+ arrs.push(_color[3])
169
+ arrs.push(p2.x)
170
+ arrs.push(p2.y)
171
+ arrs.push(p2.z)
172
+ arrs.push(_color[0])
173
+ arrs.push(_color[1])
174
+ arrs.push(_color[2])
175
+ arrs.push(_color[3])
176
+ arrs.push(p1.x)
177
+ arrs.push(p1.y)
178
+ arrs.push(_height)
179
+ arrs.push(_color[0])
180
+ arrs.push(_color[1])
181
+ arrs.push(_color[2])
182
+ arrs.push(_color[3])
183
+ arrs.push(p2.x)
184
+ arrs.push(p2.y)
185
+ arrs.push(_height)
186
+ arrs.push(_color[0])
187
+ arrs.push(_color[1])
188
+ arrs.push(_color[2])
189
+ arrs.push(_color[3])
190
+ }
191
+ _next=1;
192
+ }
193
+ }
194
+ }
195
+ return arrs;
196
+ }
197
+
198
+ drawArrayItem(item){
199
+ if(!item?.coords || !item.colors){
200
+ return;
201
+ }
202
+ // if(!item?.text){
203
+ // this.removeText(item)
204
+ // }
205
+ // if(item?.text?.type == 'layer'){
206
+ // this.drawTextByLayer(item)
207
+ // }
208
+ // if(item?.text){
209
+ // this.drawText(item)
210
+ // }
211
+ let gl = this.gl;
212
+ if(!item?.buffer) {
213
+ item.buffer = gl.createBuffer();
214
+ let _data = this.createBufferData(item);
215
+ gl.bindBuffer(gl.ARRAY_BUFFER, item.buffer);
216
+ let bufferDataArray = new Float32Array(_data);
217
+ item.bufferDataLength = _data.length;
218
+ gl.bufferData(
219
+ gl.ARRAY_BUFFER,
220
+ bufferDataArray,
221
+ gl.STATIC_DRAW
222
+ );
223
+
224
+ item.bufferColor = gl.createBuffer();
225
+ gl.bindBuffer(gl.ARRAY_BUFFER, item.bufferColor);
226
+ gl.bufferData(
227
+ gl.ARRAY_BUFFER,
228
+ bufferDataArray,
229
+ gl.STATIC_DRAW
230
+ );
231
+ item.byteSize = bufferDataArray.BYTES_PER_ELEMENT
232
+ item.aColor = gl.getAttribLocation(this.program, 'a_color');
233
+ // 每帧都要绑定 VBO,并启用 vertexAttributes、设置 vertexAttributes 的参数
234
+ //gl.bindBuffer(gl.ARRAY_BUFFER, item.buffer)
235
+ // gl.enableVertexAttribArray(this.aPos)
236
+ // gl.enableVertexAttribArray(this.aUv)
237
+ // gl.vertexAttribPointer(this.aPos, 3, gl.FLOAT, false, 20, 0)
238
+ // gl.vertexAttribPointer(this.aUv, 2, gl.FLOAT, false, 20, 12)
239
+ }
240
+ // item.buffer = gl.createBuffer()
241
+ // let bufferData = this.createBufferData(item)
242
+ // item.dataLength = bufferData.length
243
+ // gl.bufferData(
244
+ // gl.ARRAY_BUFFER,
245
+ // new Float32Array([0.8250514363610296,
246
+ // 0.41078121183273425,
247
+ // 2.99947200815501e-7,
248
+ // 0.8250521974715219,
249
+ // 0.40078065150822717,
250
+ // 2.999477853115788e-7,
251
+ // 0.8250514363610296,
252
+ // 0.41078121183273425,
253
+ // 0.02,
254
+ // 0.8250521974715219,
255
+ // 0.40078065150822717,
256
+ // 0.02]),
257
+ // gl.STATIC_DRAW
258
+ // )
259
+
260
+ gl.bindBuffer(gl.ARRAY_BUFFER, item.buffer);
261
+ gl.enableVertexAttribArray(this.aPos);
262
+ gl.vertexAttribPointer(this.aPos, 3, gl.FLOAT, false, item.byteSize *7, 0);
263
+
264
+ gl.bindBuffer(gl.ARRAY_BUFFER, item.bufferColor);
265
+ gl.enableVertexAttribArray(item.aColor);
266
+ gl.vertexAttribPointer(item.aColor, 4, gl.FLOAT, false, item.byteSize * 7, item.byteSize*3);
267
+
268
+ gl.enable(gl.BLEND);
269
+ gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
270
+ gl.drawArrays(gl.TRIANGLE_STRIP, 0, item.bufferDataLength);
271
+ }
272
+
273
+ // render(gl, matrix) {
274
+ // if(this.drawArray.length == 0 || !this.program) return;
275
+ // gl.useProgram(this.program)
276
+ // // 每帧都要传递 uniform
277
+ // gl.uniformMatrix4fv(
278
+ // gl.getUniformLocation(this.program, 'u_matrix'),
279
+ // false,
280
+ // matrix
281
+ // )
282
+ // //gl.uniform1f(gl.getUniformLocation(this.program, 'opa'),this.opacity)
283
+ // this.draw()
284
+
285
+ // // gl.useProgram(this.program)
286
+ // // // 每帧都要传递 uniform
287
+ // // gl.uniformMatrix4fv(
288
+ // // gl.getUniformLocation(this.program, 'u_matrix'),
289
+ // // false,
290
+ // // matrix
291
+ // // )
292
+ // // // 每帧都要绑定纹理参数
293
+ // // gl.bindTexture(gl.TEXTURE_2D, this.texture)
294
+ // // gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
295
+ // // gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
296
+ // // gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
297
+ // // gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
298
+
299
+ // // // 每帧都要绑定 VBO,并启用 vertexAttributes、设置 vertexAttributes 的参数
300
+ // // gl.bindBuffer(gl.ARRAY_BUFFER, this.buffer)
301
+
302
+
303
+ // // 如果你用不着透明度,可以不执行这两行
304
+ // // gl.enable(gl.BLEND)
305
+ // // gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
306
+
307
+ // // 触发绘制
308
+ // //gl.drawArrays(gl.TRIANGLES, 0, 6)
309
+ // // 每帧都要指定用哪个着色器程序
310
+ // if(this.renderBack!=null) {
311
+ // if(this.renderBack) this.renderBack()
312
+ // }
313
+ // //this.map.triggerRepaint();
314
+ // }
315
+ }
316
+
317
+
318
+
@@ -0,0 +1,13 @@
1
+ import { Popup } from "@antv/l7"
2
+ import type BaseEasyMapEvent from './EasyMapEvent'
3
+
4
+ declare namespace EasyMapComponents {
5
+ type EasyMapMarker = {
6
+ id:string,
7
+ lng:Number,
8
+ lat:Number,
9
+ popup:null | Popup,
10
+ event:null | Array<BaseEasyMapEvent>
11
+ }
12
+ }
13
+
@@ -0,0 +1,6 @@
1
+ declare namespace EasyMapEvent {
2
+ type BaseEasyMapEvent = {
3
+ type:string,
4
+ fun:Function
5
+ }
6
+ }
@@ -0,0 +1,104 @@
1
+ import gcoord from 'gcoord';
2
+ import * as turf from '@turf/turf'
3
+
4
+ export function transform(lnglat,r,coord1=gcoord.WGS84,coord2=gcoord.GCJ02)
5
+ {
6
+ if(r){
7
+ gcoord.transform(lnglat, coord1, coord2);
8
+ }
9
+ return gcoord.transform(lnglat, coord2, coord1);
10
+ }
11
+
12
+ export function midPoint(lnglat1,lnglat2){
13
+ var point1 = turf.point(lnglat1);
14
+ var point2 = turf.point(lnglat2);
15
+ return turf.midpoint(point1, point2);
16
+ }
17
+
18
+ export function hasProperty(object,props){
19
+ if(props.length == 0) throw new Error('parmas props length is 0')
20
+ for(let prop of props){
21
+ if(!object.hasOwnProperty(prop)){
22
+ throw new Error(`object property not contain ${prop}`)
23
+ }
24
+ }
25
+ }
26
+
27
+ export function GetPointByRawData(origin, distance, vAngle, hAngle)
28
+ {
29
+ let num = 6371.137;
30
+ let d = origin.lat * Math.PI / 180;
31
+ let num2 = origin.lng * Math.PI / 180;
32
+ let d2 = (Math.round(distance * Math.cos(vAngle * Math.PI / 180) * 1000)/1000) /num;
33
+ let mapPoint = {};
34
+ let d3 = hAngle * Math.PI / 180;
35
+ mapPoint.lat = Math.asin(Math.sin(d) * Math.cos(d2) + Math.cos(d) * Math.sin(d2) * Math.cos(d3));
36
+ mapPoint.lng = (num2 + Math.atan2(Math.sin(d3) * Math.sin(d2) * Math.cos(d), Math.cos(d2) - Math.sin(d) * Math.sin(mapPoint.lat))) * 180 / Math.PI;
37
+ mapPoint.lat = mapPoint.lat * 180 / Math.PI;
38
+ return mapPoint;
39
+ }
40
+
41
+ export function getLayerMapPosition(scanDis,center,vAngle=90,r = 600/600){
42
+ let dis = scanDis * Math.sqrt(2) * r// 斜边长度(公里)
43
+ let leftTop = GetPointByRawData({
44
+ lng:center[0],lat:center[1]
45
+ },dis,vAngle -90,225)
46
+ let rightBottom = GetPointByRawData({
47
+ lng:center[0],lat:center[1]
48
+ },dis,vAngle -90,45)
49
+ let bounds = [
50
+ [leftTop.lng,leftTop.lat],
51
+ [rightBottom.lng,leftTop.lat],
52
+ [rightBottom.lng,rightBottom.lat],
53
+ [leftTop.lng,rightBottom.lat]
54
+ ]
55
+ return bounds;
56
+ }
57
+
58
+ export function dataURLtoBlob(dataurl) {
59
+ var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
60
+ bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
61
+ while (n--) {
62
+ u8arr[n] = bstr.charCodeAt(n);
63
+ }
64
+ return new Blob([u8arr], { type: mime });
65
+ }
66
+
67
+ export function downloadFile(url,name){
68
+ var a = document.createElement("a")
69
+ a.setAttribute("href",url)
70
+ a.setAttribute("download",name)
71
+ a.setAttribute("target","_blank")
72
+ let clickEvent = document.createEvent("MouseEvents");
73
+ clickEvent.initEvent("click", true, true);
74
+ a.dispatchEvent(clickEvent);
75
+ }
76
+
77
+ export function downloadFileByBlob(blob,name){
78
+ let url = window.URL.createObjectURL(blob); // 生成文件在浏览器中的链接
79
+ let a = document.createElement('a');
80
+ a.href = url;
81
+ a.download = name; // 文件名
82
+ a.style.display = 'none';
83
+ document.body.appendChild(a);
84
+ a.click();
85
+ document.body.removeChild(a);
86
+ window.URL.revokeObjectURL(url); // 清除文件链接
87
+ }
88
+
89
+ export function downloadFileByBase64(base64,name){
90
+ var myBlob = dataURLtoBlob(base64)
91
+ var myUrl = URL.createObjectURL(myBlob)
92
+ downloadFile(myUrl,name)
93
+ }
94
+
95
+
96
+ /**
97
+ * 生成GUID
98
+ */
99
+ export function guid() {
100
+ function S4() {
101
+ return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
102
+ }
103
+ return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
104
+ }
@@ -0,0 +1,37 @@
1
+ const path=require('path')
2
+ module.exports={
3
+ mode:'development', // 开发模式
4
+ entry:path.resolve(__dirname,'./main.js'),
5
+ module:{
6
+ rules: [{
7
+ test: /\.css$/,
8
+ use: ['style-loader', 'css-loader']
9
+ }, {
10
+ test: /\.js$/,
11
+ exclude: /node_modules/,
12
+ use: {
13
+ loader: 'babel-loader',
14
+ options: {
15
+ presets: ['@babel/preset-env'],
16
+ },
17
+ },
18
+ },
19
+ {
20
+ test: /\.(png|jpe?g|gif|svg)$/,
21
+ use: [
22
+ {
23
+ loader: 'url-loader',
24
+ options: {
25
+ limit: 8192, // 小于8KB的图片将转换为 base64 编码,减少请求
26
+ name: './images/[name].[hash:8].[ext]',
27
+ },
28
+ },
29
+ ],
30
+ }]
31
+
32
+ },
33
+ output:{
34
+ path:path.resolve(__dirname,'./dist'),
35
+ filename:"bundle.js"
36
+ }
37
+ }