bmap.js
13 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
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('echarts')) :
typeof define === 'function' && define.amd ? define(['exports', 'echarts'], factory) :
(factory((global.bmap = {}),global.echarts));
}(this, (function (exports,echarts) { 'use strict';
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/* global BMap */
function BMapCoordSys(bmap, api) {
this._bmap = bmap;
this.dimensions = ['lng', 'lat'];
this._mapOffset = [0, 0];
this._api = api;
this._projection = new BMap.MercatorProjection();
}
BMapCoordSys.prototype.dimensions = ['lng', 'lat'];
BMapCoordSys.prototype.setZoom = function (zoom) {
this._zoom = zoom;
};
BMapCoordSys.prototype.setCenter = function (center) {
this._center = this._projection.lngLatToPoint(new BMap.Point(center[0], center[1]));
};
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]);
// TODO mercator projection is toooooooo slow
// var mercatorPoint = this._projection.lngLatToPoint(point);
// var width = this._api.getZr().getWidth();
// var height = this._api.getZr().getHeight();
// var divider = Math.pow(2, 18 - 10);
// return [
// Math.round((mercatorPoint.x - this._center.x) / divider + width / 2),
// Math.round((this._center.y - mercatorPoint.y) / divider + height / 2)
// ];
var px = this._bmap.pointToOverlayPixel(point);
var mapOffset = this._mapOffset;
return [px.x - mapOffset[0], px.y - 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.lng, pt.lat];
};
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: echarts.util.bind(this.dataToPoint, this),
size: echarts.util.bind(dataToCoordSize, this)
}
};
};
function dataToCoordSize(dataSize, dataItem) {
dataItem = dataItem || [0, 0];
return echarts.util.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;
function createOverlayCtor() {
function Overlay(root) {
this._root = root;
}
Overlay.prototype = new BMap.Overlay();
/**
* 初始化
*
* @param {BMap.Map} map
* @override
*/
Overlay.prototype.initialize = function (map) {
map.getPanes().labelPane.appendChild(this._root);
return this._root;
};
/**
* @override
*/
Overlay.prototype.draw = function () {};
return Overlay;
}
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 BMap === 'undefined') {
throw new Error('BMap api is not loaded');
}
Overlay = Overlay || createOverlayCtor();
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 BMap.Map(bmapRoot);
var overlay = new Overlay(viewportRoot);
bmap.addOverlay(overlay);
// 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);
}
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;
}
});
};
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
function v2Equal(a, b) {
return a && b && a[0] === b[0] && a[1] === b[1];
}
echarts.extendComponentModel({
type: 'bmap',
getBMap: function () {
// __bmap is injected when creating BMapCoordSys
return this.__bmap;
},
setCenterAndZoom: function (center, zoom) {
this.option.center = center;
this.option.zoom = zoom;
},
centerOrZoomChanged: function (center, zoom) {
var option = this.option;
return !(v2Equal(center, option.center) && zoom === option.zoom);
},
defaultOption: {
center: [104.114129, 37.550339],
zoom: 5,
mapStyle: {},
roam: false
}
});
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
echarts.extendComponentView({
type: 'bmap',
render: function (bMapModel, ecModel, api) {
var rendering = true;
var bmap = bMapModel.getBMap();
var viewportRoot = api.getZr().painter.getViewportRoot();
var coordSys = bMapModel.coordinateSystem;
var moveHandler = function (type, target) {
if (rendering) {
return;
}
var offsetEl = viewportRoot.parentNode.parentNode.parentNode;
var mapOffset = [
-parseInt(offsetEl.style.left, 10) || 0,
-parseInt(offsetEl.style.top, 10) || 0
];
viewportRoot.style.left = mapOffset[0] + 'px';
viewportRoot.style.top = mapOffset[1] + 'px';
coordSys.setMapOffset(mapOffset);
bMapModel.__mapOffset = mapOffset;
api.dispatchAction({
type: 'bmapRoam'
});
};
function zoomEndHandler() {
if (rendering) {
return;
}
api.dispatchAction({
type: 'bmapRoam'
});
}
bmap.removeEventListener('moving', this._oldMoveHandler);
// FIXME
// Moveend may be triggered by centerAndZoom method when creating coordSys next time
// bmap.removeEventListener('moveend', this._oldMoveHandler);
bmap.removeEventListener('zoomend', this._oldZoomEndHandler);
bmap.addEventListener('moving', moveHandler);
// bmap.addEventListener('moveend', moveHandler);
bmap.addEventListener('zoomend', zoomEndHandler);
this._oldMoveHandler = moveHandler;
this._oldZoomEndHandler = zoomEndHandler;
var roam = bMapModel.get('roam');
if (roam && roam !== 'scale') {
bmap.enableDragging();
}
else {
bmap.disableDragging();
}
if (roam && roam !== 'move') {
bmap.enableScrollWheelZoom();
bmap.enableDoubleClickZoom();
bmap.enablePinchToZoom();
}
else {
bmap.disableScrollWheelZoom();
bmap.disableDoubleClickZoom();
bmap.disablePinchToZoom();
}
var originalStyle = bMapModel.__mapStyle;
var newMapStyle = bMapModel.get('mapStyle') || {};
// FIXME, Not use JSON methods
var mapStyleStr = JSON.stringify(newMapStyle);
if (JSON.stringify(originalStyle) !== mapStyleStr) {
// FIXME May have blank tile when dragging if setMapStyle
if (Object.keys(newMapStyle).length) {
bmap.setMapStyle(newMapStyle);
}
bMapModel.__mapStyle = JSON.parse(mapStyleStr);
}
rendering = false;
}
});
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/**
* BMap component extension
*/
echarts.registerCoordinateSystem('bmap', BMapCoordSys);
// Action
echarts.registerAction({
type: 'bmapRoam',
event: 'bmapRoam',
update: 'updateLayout'
}, function (payload, ecModel) {
ecModel.eachComponent('bmap', function (bMapModel) {
var bmap = bMapModel.getBMap();
var center = bmap.getCenter();
bMapModel.setCenterAndZoom([center.lng, center.lat], bmap.getZoom());
});
});
var version = '1.0.0';
exports.version = version;
})));
//# sourceMappingURL=bmap.js.map