forked from huiyan-fe/mapv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapv.js
More file actions
2914 lines (2475 loc) · 83.7 KB
/
Copy pathMapv.js
File metadata and controls
2914 lines (2475 loc) · 83.7 KB
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!function(){
var Mapv;
;var util = {
isPlainObject: function (obj) {
var key;
var class2type = {};
var hasOwn = class2type.hasOwnProperty;
// Must be an Object.
// Because of IE, we also have to check the presence of the constructor property.
// Make sure that DOM nodes and window objects don't pass through, as well
if (!obj || typeof obj !== 'object' || obj.nodeType) {
return false;
}
// Not own constructor property must be Object
var hasNoOwn = !hasOwn.call(obj, 'constructor');
var hasNoOwnPrototypeOf = !hasOwn.call(obj.constructor.prototype, 'isPrototypeOf');
if (obj.constructor && hasNoOwn && hasNoOwnPrototypeOf) {
return false;
}
// Own properties are enumerated firstly, so to speed up,
// if last one is own, then all properties are own.
for (key in obj) {}
return key === undefined || hasOwn.call(obj, key);
},
/**
* 深度扩展一个对象
*/
extend: function (destination, source) {
var i,
toStr = Object.prototype.toString,
astr = '[object Array]';
destination = destination || {};
for (i in source) {
if (source.hasOwnProperty(i)) {
if (util.isPlainObject(source[i])) {
destination[i] = (toStr.call(source[i]) === astr) ? [] : {};
arguments.callee(destination[i], source[i]);
destination[i] = source[i];
} else {
destination[i] = source[i];
}
}
}
return destination;
},
/**
* copy an object
* @param {Object} obj the obj
* @return {Object} new object
*/
copy: function (obj) {
return this.extend({}, obj);
},
/**
* 为类型构造器建立继承关系
* @name baidu.lang.inherits
* @function
* @grammar baidu.lang.inherits(subClass, superClass[, className])
* @param {Function} subClass 子类构造器
* @param {Function} superClass 父类构造器
* @remark
*
使subClass继承superClass的prototype,因此subClass的实例能够使用superClass的prototype中定义的所有属性和方法。<br>
这个函数实际上是建立了subClass和superClass的原型链集成,并对subClass进行了constructor修正。<br>
<strong>注意:如果要继承构造函数,需要在subClass里面call一下,具体见下面的demo例子</strong>
* @shortcut inherits
* @meta standard
*/
inherits: function (subClass, superClass) {
var key;
var proto;
var selfProps = subClass.prototype;
var Clazz = new Function();
Clazz.prototype = superClass.prototype;
proto = subClass.prototype = new Clazz();
for (key in selfProps) {
proto[key] = selfProps[key];
}
subClass.prototype.constructor = subClass;
subClass.superClass = superClass.prototype;
},
// 在页面中添加样式
addCssByStyle: function (cssString) {
var doc = document;
var style = doc.createElement('style');
style.setAttribute('type', 'text/css');
if (style.styleSheet) { // IE
style.styleSheet.cssText = cssString;
} else { // w3c
var cssText = doc.createTextNode(cssString);
style.appendChild(cssText);
}
var heads = doc.getElementsByTagName('head');
if (heads.length) {
heads[0].appendChild(style);
} else {
doc.documentElement.appendChild(style);
}
}
}
;var MVCObject;
(function() {
function Accessor(target, targetKey) {
var self = this;
self.target = target;
self.targetKey = targetKey;
}
Accessor.prototype.transform = function(from, to) {
var self = this;
self.from = from;
self.to = to;
self.target.notify(self.targetKey);
return self;
}
MVCObject = (function() {
var getterNameCache = {};
var setterNameCache = {};
var uuid = 0;
var bindings = '__bindings__';
var accessors = '__accessors__';
var uid = '__uid__';
function capitalize(str) {
return str.substr(0, 1).toUpperCase() + str.substr(1);
}
function getUid(obj) {
return obj[uid] || (obj[uid] = ++uuid);
}
function toKey(key) {
return '_' + key;
}
function getGetterName(key) {
if (getterNameCache.hasOwnProperty(key)) {
return getterNameCache[key];
} else {
return getterNameCache[key] = 'get' + capitalize(key);
}
}
function getSetterName(key) {
if (setterNameCache.hasOwnProperty(key)) {
return setterNameCache[key];
} else {
return setterNameCache[key] = 'set' + capitalize(key);
}
}
/**
* @description 这个函数的触发需要时机
* 在一个key所在的终端对象遍历到时触发
* 同时传播给所有直接、间接监听targetKey的对象
* 在调用MVCObject的set方法时开始遍历
*
* @param target {MVCObject} 继承了MVCObject的对象
* @param targetKey {String} 当前对象中被监听的字段
* @return {void}
*/
function triggerChange(target, targetKey) {
var evt = targetKey + '_changed';
/**
* 优先检测并执行目标对象key对应的响应方法
* 其次检测并执行默认方法
*/
if (target[evt]) {
target[evt]();
} else if (typeof target.changed === 'function') {
target.changed(targetKey);
}
if (target[bindings] && target[bindings][targetKey]) {
var ref = target[bindings][targetKey];
var bindingObj, bindingUid;
for (bindingUid in ref) {
if (ref.hasOwnProperty(bindingUid)) {
bindingObj = ref[bindingUid];
triggerChange(bindingObj.target, bindingObj.targetKey);
}
}
}
}
function MVCObject() {};
var proto = MVCObject.prototype;
/**
* @description 从依赖链中获取对应key的值
* @param {String} key 关键值
* @return {mixed} 对应的值
*/
proto.get = function(key) {
var self = this;
if (self[accessors] && self[accessors].hasOwnProperty(key)) {
var accessor = self[accessors][key];
var targetKey = accessor.targetKey;
var target = accessor.target;
var getterName = getGetterName(targetKey);
var value;
if (target[getterName]) {
value = target[getterName]();
} else {
value = target.get(targetKey);
}
if (accessor.to) {
value = accessor.to(value);
}
} else if (self.hasOwnProperty(toKey(key))) {
value = self[toKey(key)];
}
return value;
};
/**
* @description set方法遍历依赖链直到找到key的持有对象设置key的值;
* 有三个分支
* @param {String} key 关键值
* @param {all} value 要给key设定的值,可以是所有类型
* @return {this}
*/
proto.set = function(key, value) {
var self = this;
if (self[accessors] && self[accessors].hasOwnProperty(key)) {
var accessor = self[accessors][key];
var targetKey = accessor.targetKey;
var target = accessor.target;
var setterName = getSetterName(targetKey);
if (accessor.from) {
value = accessor.from(value);
}
if (target[setterName]) {
target[setterName](value);
} else {
target.set(targetKey, value);
}
} else {
this[toKey(key)] = value;
triggerChange(self, key);
}
return self;
};
/**
* @description 没个MVCObject对象各自的响应对应的key值变化时的逻辑
*/
proto.changed = function() {};
/**
* @description 手动触发对应key的事件传播
* @param {String} key 关键值
* @return {this}
*/
proto.notify = function(key) {
var self = this;
if (self[accessors] && self[accessors].hasOwnProperty(key)) {
var accessor = self[accessors][key];
var targetKey = accessor.targetKey;
var target = accessor.target;
target.notify(targetKey);
} else {
triggerChange(self, key);
}
return self;
};
proto.setValues = function(values) {
var self = this;
var key, setterName, value;
for (key in values) {
if (values.hasOwnProperty(key)) {
value = values[key];
setterName = getSetterName(key);
if (self[setterName]) {
self[setterName](value);
} else {
self.set(key, value);
}
}
}
return self;
};
proto.setOptions = proto.setValues;
/**
* @description 将当前对象的一个key与目标对象的targetKey建立监听和广播关系
* @param key {String} 当前对象上的key
* @param target {Object} 目标对象
* @param tarrgetKey {String} 目标对象上的key
* @param noNotify {Boolean}
* @return {Accessor}
*/
proto.bindTo = function(key, target, targetKey, noNotify) {
targetKey || (targetKey = key);
var self = this;
self.unbind(key);
self[accessors] || (self[accessors] = {});
target[bindings] || (target[bindings] = {});
target[bindings][targetKey] || (target[bindings][targetKey] = {});
var binding = new Accessor(self, key);
var accessor = new Accessor(target, targetKey);
self[accessors][key] = accessor;
target[bindings][targetKey][getUid(self)] = binding;
if (!noNotify) {
triggerChange(self, key);
}
return accessor;
};
/**
* @description 解除当前对象上key与目标对象的监听
* @param {String} key 关键字
* @return {this}
*/
proto.unbind = function(key) {
var self = this;
if (self[accessors]) {
var accessor = self[accessors][key];
if (accessor) {
var target = accessor.target;
var targetKey = accessor.targetKey;
self[toKey(key)] = self.get(key);
delete target[bindings][targetKey][getUid(self)];
delete self[accessors][key];
}
}
return self;
}
proto.unbindAll = function() {
var self = this;
if (self[accessors]) {
var ref = self[accessors];
for (var key in ref) {
if (ref.hasOwnProperty(key)) {
self.unbind(key);
}
}
}
return self;
};
proto.initOptions = function(options) {
for (var key in options) {
this[getGetterName(key)] = (function(key) {
return function () {
return this.get(key);
}
})(key);
this[getSetterName(key)] = (function(key) {
return function (value) {
this.set(key, value);
}
})(key);
this[toKey(key)] = options[key];
}
}
return MVCObject;
})();
})();
;function Class () {
this.__listeners = {}; // 存储自定义事件对象
}
util.inherits(Class, MVCObject);
/**
* 注册对象的事件监听器
* @grammar obj.addEventListener(type, handler[, key])
* @param {string} type 自定义事件的名称
* @param {Function} handler 自定义事件被触发时应该调用的回调函数
* @remark 事件类型区分大小写。如果自定义事件名称不是以小写"on"开头,该方法会给它加上"on"再进行判断,即"click"和"onclick"会被认为是同一种事件。
*/
Class.prototype.addEventListener = function (type, handler) {
typeof this.__listeners[type] != "object" && (this.__listeners[type] = []);
this.__listeners[type].push(handler);
return this;
}
/**
* 移除对象的事件监听器。
* @grammar obj.removeEventListener(type, handler)
* @param {string} type 事件类型
* @param {Function} handler 要移除的事件监听函数
* @remark 如果第二个参数handler没有被绑定到对应的自定义事件中,什么也不做。
*/
Class.prototype.removeEventListener = function (type, handler) {
var fns = this.__listeners[type];
if (!fns) {
return false;
}
for (var i = fns.length; i >= 0; i--) {
if (fns[i] === handler) {
fns.splice(i, 1);
}
}
return this;
};
/**
* 派发自定义事件,使得绑定到自定义事件上面的函数都会被执行
* @grammar obj.dispatchEvent(event, options)
* @param {String} 事件名称
* @param {Object} options 扩展参数
*/
Class.prototype.dispatchEvent = function (type, options) {
var event = util.extend({}, options);
var fns = this.__listeners[type];
if (!fns) {
return false;
}
for (var i = fns.length - 1; i >= 0; i--) {
fns[i].call(this, event);
}
return this;
}
Class.prototype.dispose = function () {
}
;/* globals Layer GeoData DrawTypeControl OptionalData util DataControl DrawScale DataRangeControl*/
/**
* @param {Object}
*/
function Mapv(options) {
Class.call(this);
this.initOptions({
map: null, //地图参数
dataRangeCtrol: null
});
this.setOptions(options);
this._layers = [];
this._initDrawScale();
// this._initDataRange();
// this._initDrawTypeControl();
}
util.inherits(Mapv, Class);
Mapv.prototype._initDrawScale = function () {
this.Scale = new DrawScale();
};
Mapv.prototype._initDataRange = function () {
this.setDataRangeCtrol(new DataRangeControl());
this.getMap().addControl(this.getDataRangeCtrol());
}
Mapv.prototype._initDrawTypeControl = function () {
this._drawTypeControl = new DrawTypeControl({
mapv: this
});
this.getMap().addControl(this._drawTypeControl);
};
;function Layer (options) {
Class.call(this);
this._drawer = {};
this.initOptions($.extend({
ctx: null,
animationCtx: null,
mapv: null,
map: null,
data: [],
dataType: 'point',
animationOptions: {
radius: 5
},
coordType: 'bd09ll',
drawType: 'simple',
animation: false,
geometry: null,
zIndex: 1
}, options));
this.notify('data');
this.notify('mapv');
}
util.inherits(Layer, Class);
util.extend(Layer.prototype, {
initialize: function () {
if (this.mapMask) {
return;
}
this.setMap(this.getMapv().getMap());
this.bindTo('map', this.getMapv());
this.mapMask = new MapMask({
map: this.getMapv().getMap(),
zIndex: this.getZIndex(),
elementTag: "canvas"
});
this.setCtx(this.mapMask.getContainer().getContext("2d"));
var that = this;
this.mapMask.addEventListener('draw', function () {
that.draw();
});
if (this.getAnimation()) {
this.animationMask = new MapMask({
map: this.getMapv().getMap(),
zIndex: this.getZIndex(),
elementTag: "canvas"
});
this.setAnimationCtx(this.animationMask.getContainer().getContext("2d"));
}
},
draw: function () {
var ctx = this.getCtx();
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
this._calculatePixel();
this._getDrawer().drawMap();
if (this.getDataType() === 'polyline' && this.getAnimation() && !this._animationFlag) {
this.drawAnimation();
this._animationFlag = true;
}
},
drawAnimation: function () {
var animationCtx = this.getAnimationCtx();
animationCtx.clearRect(0, 0, animationCtx.canvas.width, animationCtx.canvas.height);
var that = this;
this._getDrawer().drawAnimation();
if (this.getAnimation()) {
requestAnimationFrame(function () {
that.drawAnimation();
});
}
},
animation_changed: function () {
if (this.getAnimation()) {
this.drawAnimation();
}
},
mapv_changed: function () {
if (!this.getMapv()) {
this.mapMask && this.mapMask.hide();
return;
} else {
this.mapMask && this.mapMask.show();
}
this.initialize();
this.updateControl();
this.draw();
},
drawType_changed: function () {
this.updateControl();
this.draw();
},
drawOptions_changed: function () {
this.draw();
},
updateControl: function () {
var mapv = this.getMapv();
var drawer = this._getDrawer();
if (drawer.drawDataRange) {
map.addControl(mapv.getDataRangeCtrol());
drawer.drawDataRange(mapv.getDataRangeCtrol().getContainer());
} else {
map.removeControl(mapv.getDataRangeCtrol());
}
// for drawer scale
if(drawer.scale) {
drawer.scale(mapv.Scale);
mapv.Scale.show();
} else {
mapv.Scale.hide();
}
// mapv._drawTypeControl.showLayer(this);
this.getMapv().OptionalData && this.getMapv().OptionalData.initController(this, this.getDrawType());
},
_getDrawer: function () {
var drawType = this.getDrawType();
if (!this._drawer[drawType]) {
var funcName = drawType.replace(/(\w)/, function (v) {
return v.toUpperCase();
});
funcName += 'Drawer';
var drawer = this._drawer[drawType] = eval('(new ' + funcName + '(this))');
drawer.setDrawOptions(this.getDrawOptions()/*[drawType]*/);
if (drawer.scale) {
drawer.scale(this.getMapv().Scale);
this.getMapv().Scale.show();
} else {
this.getMapv().Scale.hide();
}
}
return this._drawer[drawType];
},
_calculatePixel: function () {
var map = this.getMapv().getMap();
var mercatorProjection = map.getMapType().getProjection();
// 墨卡托坐标计算方法
var zoom = map.getZoom();
var zoomUnit = Math.pow(2, 18 - zoom);
var mcCenter = mercatorProjection.lngLatToPoint(map.getCenter());
var nwMc = new BMap.Pixel(mcCenter.x - (map.getSize().width / 2) * zoomUnit,
mcCenter.y + (map.getSize().height / 2) * zoomUnit); //左上角墨卡托坐标
var data = this.getData();
var map = this.getMap();
for (var j = 0; j < data.length; j++) {
if (data[j].lng && data[j].lat) {
var pixel = map.pointToPixel(new BMap.Point(data[j].lng, data[j].lat));
data[j].px = pixel.x;
data[j].py = pixel.y;
}
if (data[j].x && data[j].y) {
data[j].px = (data[j].x - nwMc.x) / zoomUnit;
data[j].py = (nwMc.y - data[j].y) / zoomUnit;
}
if (data[j].geo) {
var tmp = [];
if (this.getCoordType() === 'bd09ll') {
for (var i = 0; i < data[j].geo.length; i++) {
var pixel = map.pointToPixel(new BMap.Point(data[j].geo[i][0], data[j].geo[i][1]));
tmp.push([pixel.x, pixel.y]);
}
} else if (this.getCoordType() === 'bd09mc') {
for (var i = 0; i < data[j].geo.length; i++) {
tmp.push([(data[j].geo[i][0] - nwMc.x) / zoomUnit, (nwMc.y - data[j].geo[i][1]) / zoomUnit]);
}
}
data[j].pgeo = tmp;
}
}
},
data_changed: function () {
var data = this.getData();
if (!data || data.length < 1) {
return;
}
if (this.getDataType() === "polyline" && this.getAnimation()) {
for (var i = 0; i < data.length; i++) {
data[i].index = parseInt(Math.random() * data[i].geo.length, 10);
}
}
this._min = data[0].count;
this._max = data[0].count;
for (var i = 0; i < data.length; i++) {
this._max = Math.max(this._max, data[i].count);
this._min = Math.min(this._min, data[i].count);
}
},
getDataRange: function () {
return {
min: this._min,
max: this._max
};
}
});
util.extend(Mapv.prototype, {
addLayer: function (layer) {
this._layers.push(layer);
layer._layerAdd(this);
},
removeLayer: function (layer) {
for (var i = this._layers.length--; i >= 0; i--) {
if (this._layers[i] === layer) {
this._layers.splice(i, 1);
}
}
}
});
;function MapMask(options){
this.options = options || {};
this.initElement();
this._map = options.map;
this.show();
}
MapMask.prototype = new BMap.Overlay();
MapMask.prototype.initialize = function(map){
this._map = map;
var elementTag = this.options.elementTag || "div";
var element = this.element = document.createElement(elementTag);
var size = map.getSize();
element.width = size.width;
element.height = size.height;
element.style.cssText = "position:absolute;"
+ "left:0;"
+ "top:0;"
+ "width:" + size.width + "px;"
+ "height:" + size.height + "px";
if (this.options.zIndex !== undefined) {
element.style.zIndex = this.options.zIndex;
}
map.getPanes().labelPane.appendChild(this.element);
return this.element;
}
MapMask.prototype.initElement = function(map){
}
MapMask.prototype.draw = function(){
var map = this._map;
var bounds = map.getBounds();
var sw = bounds.getSouthWest();
var ne = bounds.getNorthEast();
var pixel = map.pointToOverlayPixel(new BMap.Point(sw.lng, ne.lat));
this.element.style.left = pixel.x + "px";
this.element.style.top = pixel.y + "px";
this.dispatchEvent('draw');
}
MapMask.prototype.getContainer = function(){
return this.element;
}
MapMask.prototype.show = function(){
this._map.addOverlay(this);
}
MapMask.prototype.hide = function(){
this._map.removeOverlay(this);
}
;function DataControl(superObj) {
this.initDom();
this.initEvent();
this.super = superObj;
this.geoData = superObj.geoData;
// console.log(this.geoData.setData);
}
DataControl.prototype.initDom = function () {
var control = this.control = document.createElement('div');
var input = this.input = document.createElement('input');
input.type = 'file';
var tipstitle = document.createElement('div');
tipstitle.textContent = '自定义数据:';
var tips = document.createElement('div');
tips.textContent = '拖拽文件到窗口或者选择自定义文件';
control.appendChild(tipstitle);
control.appendChild(tips);
control.appendChild(input);
control.style.fontSize = '12px';
control.style.lineHeight = '1.8em';
control.style.position = 'absolute';
control.style.bottom = '50px';
control.style.left = '10px';
control.style.padding = '10px 20px';
control.style.color = '#FFF';
control.style.background = 'rgba(0,0,0,0.5)';
control.style.zIndex = '100000';
control.style.overflow = 'hidden';
control.style.webkitTransition = 'all 0.5s ease-in';
var history = document.createElement('div');
var historyTitle = document.createElement('div');
historyTitle.textContent = '历史数据';
this.history = document.createElement('div');
history.appendChild(historyTitle);
history.appendChild(this.history);
control.appendChild(history);
document.body.appendChild(control);
};
DataControl.prototype.initEvent = function () {
var self = this;
var reader = new FileReader();
reader.addEventListener('load', function (e) {
var text = reader.result;
var draw = formatRender(text);
if (draw) {
var filenames = localStorage.getItem('filenames') || '{}';
filenames = JSON.parse(filenames);
if (!reader.fileName || !reader.fileSize) {
console.log('no fileName or fileSize , save faild ');
return false;
}
for (var i in filenames) {
if (filenames[i].name === this.fileName && filenames[i].size === this.fileSize) {
return false;
}
}
var saveName = this.fileName + this.fileSize + parseInt(Math.random() * 1e17, 10).toString(36);
filenames[saveName] = {
size: this.fileSize,
name: this.fileName
};
// console.log(filenames)
localStorage.setItem('filenames', JSON.stringify(filenames));
localStorage.setItem(saveName, JSON.stringify(text));
self.initHistory();
}
});
self.history.addEventListener('click', function (e) {
var node = e.target;
if (node.nodeName === 'A') {
var storageName = node.getAttribute('storageName');
var dataStr = localStorage.getItem(storageName);
formatRender(dataStr);
}
return false;
});
self.input.addEventListener('change', function (e) {
reader.readAsText(e.target.files[0]);
reader.fileName = e.target.files[0].name;
reader.fileSize = e.target.files[0].size;
});
document.addEventListener('dragover', function (event) {
event.preventDefault();
}, false);
document.addEventListener('drop', function (event) {
event.preventDefault();
reader.readAsText(event.dataTransfer.files[0]);
reader.fileName = event.dataTransfer.files[0].name;
reader.fileSize = event.dataTransfer.files[0].size;
return false;
});
function formatRender(dataStr) {
var data;
var wrongType = false;
try {
data = JSON.parse(dataStr.replace(/\s/g, ''));
// console.log('??!@',data)
var count = 0;
while (typeof (data) === 'string' && count <= 10) {
data = JSON.parse(data);
count++;
}
wrongType = false;
} catch (e) {
wrongType = true;
}
if (wrongType) {
try {
data = [];
var dataT = dataStr.split('\n');
if (dataT.length <= 1) {
dataT = dataStr.split('\\n');
}
var keys = dataT[0].split(',');
// console.log(keys)
for (var i = 1; i < keys.length; i++) {
var values = dataT[i].split(',');
var obj = {};
var nonameIndex = 0;
for (var j = 0; j < values.length; j++) {
var name = keys[j] || 'noname' + (nonameIndex++);
name = name.replace(/\\r/g, '');
obj[name] = Number(values[j].replace(/\\r/g, '').replace(/\"/g, ''));
}
data.push(obj);
}
data = JSON.stringify(data).replace(/\\r/g, '');
data = JSON.parse(data);
wrongType = false;
} catch (e) {
window.console.log(e);
wrongType = true;
}
}
if (wrongType) {
alert('数据格式错误,请检查是否为json或者csv格式数据');
return false;
}
self.geoData.setData(data);
console.log(self.super._layers)
for(var i=0;i<self.super._layers.length;i++){
self.super._layers[i].draw();
}
return true;
}
};
;function DataRangeControl(){
// 默认停靠位置和偏移量
this.defaultAnchor = BMAP_ANCHOR_BOTTOM_RIGHT;
this.defaultOffset = new BMap.Size(10, 10);
}
DataRangeControl.prototype = new BMap.Control();
DataRangeControl.prototype.initialize = function(map){
var canvas = this.canvas = document.createElement("canvas");
canvas.style.background = "#fff";
canvas.style.boxShadow = "rgba(0,0,0,0.2) 0 0 4px 2px";
canvas.style.border = "1px solid #999999";
canvas.style.borderRadius = "4px";
// 添加DOM元素到地图中
map.getContainer().appendChild(canvas);
// 将DOM元素返回
return canvas;
}
DataRangeControl.prototype.getContainer = function(map){
return this.canvas;
}
;function DrawScale() {
this.init();
this._Event();
}
DrawScale.prototype.change = function (callback) {
var self = this;
self.changeFn = callback;
};
DrawScale.prototype.hide = function () {
var self = this;
self.box.style.display = 'none';
};
DrawScale.prototype.show = function () {
var self = this;
self.box.style.display = 'block';
};
DrawScale.prototype.set = function (obj) {
var self = this;
self.max = obj.max || self.max;
self.min = obj.min || self.min;