-
Notifications
You must be signed in to change notification settings - Fork 0
/
BMapCoordSys.js
181 lines (154 loc) · 5.95 KB
/
BMapCoordSys.js
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
define(function (require) {
var echarts = require('echarts');
var zrUtil = echarts.util;
function BMapCoordSys(bmap, api) {
this._bmap = bmap;
this.dimensions = ['lng', 'lat'];
this._mapOffset = [0, 0];
this._api = api;
}
BMapCoordSys.prototype.dimensions = ['lng', 'lat'];
BMapCoordSys.prototype.setZoom = function (zoom) {
this._zoom = zoom;
};
BMapCoordSys.prototype.setCenter = function (center) {
this._center = center;
};
BMapCoordSys.prototype.setMapOffset = function (mapOffset) {
this._mapOffset = mapOffset;
};
BMapCoordSys.prototype.getBMap = function () {
return this._bmap;
};
BMapCoordSys.prototype.dataToPoint = function (data) {
//var point = new BMap.Point(data[0], data[1]);
//var px = this._bmap.pointToOverlayPixel(point);
var mapOffset = this._mapOffset;
return [data[0] - mapOffset[0], data[1] - mapOffset[1]];
};
BMapCoordSys.prototype.pointToData = function (pt) {
var mapOffset = this._mapOffset;
// var pt = this._bmap.overlayPixelToPoint({
// x: pt[0] + mapOffset[0],
// y: pt[1] + mapOffset[1]
// });
return [pt[0], pt[1]];
};
BMapCoordSys.prototype.getViewRect = function () {
var api = this._api;
return new echarts.graphic.BoundingRect(0, 0, api.getWidth(), api.getHeight());
};
BMapCoordSys.prototype.getRoamTransform = function () {
return echarts.matrix.create();
};
BMapCoordSys.prototype.prepareCustoms = function (data) {
var rect = this.getViewRect();
return {
coordSys: {
// The name exposed to user is always 'cartesian2d' but not 'grid'.
type: 'bmap',
x: rect.x,
y: rect.y,
width: rect.width,
height: rect.height
},
api: {
coord: zrUtil.bind(this.dataToPoint, this),
size: zrUtil.bind(dataToCoordSize, this)
}
};
};
function dataToCoordSize(dataSize, dataItem) {
dataItem = dataItem || [0, 0];
return zrUtil.map([0, 1], function (dimIdx) {
var val = dataItem[dimIdx];
var halfSize = dataSize[dimIdx] / 2;
var p1 = [];
var p2 = [];
p1[dimIdx] = val - halfSize;
p2[dimIdx] = val + halfSize;
p1[1 - dimIdx] = p2[1 - dimIdx] = dataItem[1 - dimIdx];
return Math.abs(this.dataToPoint(p1)[dimIdx] - this.dataToPoint(p2)[dimIdx]);
}, this);
}
var Overlay;
// For deciding which dimensions to use when creating list data
BMapCoordSys.dimensions = BMapCoordSys.prototype.dimensions;
BMapCoordSys.create = function (ecModel, api) {
var bmapCoordSys;
var root = api.getDom();
// TODO Dispose
ecModel.eachComponent('bmap', function (bmapModel) {
var painter = api.getZr().painter;
var viewportRoot = painter.getViewportRoot();
if (typeof ol === 'undefined') {
throw new Error('ol api is not loaded');
}
if (bmapCoordSys) {
throw new Error('Only one bmap component can exist');
}
if (!bmapModel.__bmap) {
// Not support IE8
var bmapRoot = root.querySelector('.ec-extension-bmap');
if (bmapRoot) {
// Reset viewport left and top, which will be changed
// in moving handler in BMapView
viewportRoot.style.left = '0px';
viewportRoot.style.top = '0px';
root.removeChild(bmapRoot);
}
bmapRoot = document.createElement('div');
bmapRoot.style.cssText = 'width:100%;height:100%';
// Not support IE8
bmapRoot.classList.add('ec-extension-bmap');
root.appendChild(bmapRoot);
var bmap = bmapModel.__bmap = new ol.Map({
view: new ol.View({
center: [0, 0],
zoom: 1
}),
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
target: bmapRoot
});
var imageCanvas=new ol.source.ImageCanvas({
canvasFunction: function() { return viewportRoot;},
});
var canvasLayer = new ol.layer.Image({
source: imageCanvas,
});
bmap.addLayer(canvasLayer);
// Override
painter.getViewportRootOffset = function () {
return {offsetLeft: 0, offsetTop: 0};
};
}
var bmap = bmapModel.__bmap;
// Set bmap options
// centerAndZoom before layout and render
var center = bmapModel.get('center');
var zoom = bmapModel.get('zoom');
if (center && zoom) {
// === var pt = new BMap.Point(center[0], center[1]);
// == bmap.centerAndZoom(pt, zoom);
var view = bmap.getView();
view.setCenter(center);
view.setZoom(zoom);
}
bmapCoordSys = new BMapCoordSys(bmap, api);
bmapCoordSys.setMapOffset(bmapModel.__mapOffset || [0, 0]);
bmapCoordSys.setZoom(zoom);
bmapCoordSys.setCenter(center);
bmapModel.coordinateSystem = bmapCoordSys;
});
ecModel.eachSeries(function (seriesModel) {
if (seriesModel.get('coordinateSystem') === 'bmap') {
seriesModel.coordinateSystem = bmapCoordSys;
}
});
};
return BMapCoordSys;
});