bmap.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('echarts')) :
  3. typeof define === 'function' && define.amd ? define(['exports', 'echarts'], factory) :
  4. (factory((global.bmap = {}),global.echarts));
  5. }(this, (function (exports,echarts) { 'use strict';
  6. /*
  7. * Licensed to the Apache Software Foundation (ASF) under one
  8. * or more contributor license agreements. See the NOTICE file
  9. * distributed with this work for additional information
  10. * regarding copyright ownership. The ASF licenses this file
  11. * to you under the Apache License, Version 2.0 (the
  12. * "License"); you may not use this file except in compliance
  13. * with the License. You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing,
  18. * software distributed under the License is distributed on an
  19. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  20. * KIND, either express or implied. See the License for the
  21. * specific language governing permissions and limitations
  22. * under the License.
  23. */
  24. /* global BMap */
  25. function BMapCoordSys(bmap, api) {
  26. this._bmap = bmap;
  27. this.dimensions = ['lng', 'lat'];
  28. this._mapOffset = [0, 0];
  29. this._api = api;
  30. this._projection = new BMap.MercatorProjection();
  31. }
  32. BMapCoordSys.prototype.dimensions = ['lng', 'lat'];
  33. BMapCoordSys.prototype.setZoom = function (zoom) {
  34. this._zoom = zoom;
  35. };
  36. BMapCoordSys.prototype.setCenter = function (center) {
  37. this._center = this._projection.lngLatToPoint(new BMap.Point(center[0], center[1]));
  38. };
  39. BMapCoordSys.prototype.setMapOffset = function (mapOffset) {
  40. this._mapOffset = mapOffset;
  41. };
  42. BMapCoordSys.prototype.getBMap = function () {
  43. return this._bmap;
  44. };
  45. BMapCoordSys.prototype.dataToPoint = function (data) {
  46. var point = new BMap.Point(data[0], data[1]);
  47. // TODO mercator projection is toooooooo slow
  48. // var mercatorPoint = this._projection.lngLatToPoint(point);
  49. // var width = this._api.getZr().getWidth();
  50. // var height = this._api.getZr().getHeight();
  51. // var divider = Math.pow(2, 18 - 10);
  52. // return [
  53. // Math.round((mercatorPoint.x - this._center.x) / divider + width / 2),
  54. // Math.round((this._center.y - mercatorPoint.y) / divider + height / 2)
  55. // ];
  56. var px = this._bmap.pointToOverlayPixel(point);
  57. var mapOffset = this._mapOffset;
  58. return [px.x - mapOffset[0], px.y - mapOffset[1]];
  59. };
  60. BMapCoordSys.prototype.pointToData = function (pt) {
  61. var mapOffset = this._mapOffset;
  62. var pt = this._bmap.overlayPixelToPoint({
  63. x: pt[0] + mapOffset[0],
  64. y: pt[1] + mapOffset[1]
  65. });
  66. return [pt.lng, pt.lat];
  67. };
  68. BMapCoordSys.prototype.getViewRect = function () {
  69. var api = this._api;
  70. return new echarts.graphic.BoundingRect(0, 0, api.getWidth(), api.getHeight());
  71. };
  72. BMapCoordSys.prototype.getRoamTransform = function () {
  73. return echarts.matrix.create();
  74. };
  75. BMapCoordSys.prototype.prepareCustoms = function (data) {
  76. var rect = this.getViewRect();
  77. return {
  78. coordSys: {
  79. // The name exposed to user is always 'cartesian2d' but not 'grid'.
  80. type: 'bmap',
  81. x: rect.x,
  82. y: rect.y,
  83. width: rect.width,
  84. height: rect.height
  85. },
  86. api: {
  87. coord: echarts.util.bind(this.dataToPoint, this),
  88. size: echarts.util.bind(dataToCoordSize, this)
  89. }
  90. };
  91. };
  92. function dataToCoordSize(dataSize, dataItem) {
  93. dataItem = dataItem || [0, 0];
  94. return echarts.util.map([0, 1], function (dimIdx) {
  95. var val = dataItem[dimIdx];
  96. var halfSize = dataSize[dimIdx] / 2;
  97. var p1 = [];
  98. var p2 = [];
  99. p1[dimIdx] = val - halfSize;
  100. p2[dimIdx] = val + halfSize;
  101. p1[1 - dimIdx] = p2[1 - dimIdx] = dataItem[1 - dimIdx];
  102. return Math.abs(this.dataToPoint(p1)[dimIdx] - this.dataToPoint(p2)[dimIdx]);
  103. }, this);
  104. }
  105. var Overlay;
  106. // For deciding which dimensions to use when creating list data
  107. BMapCoordSys.dimensions = BMapCoordSys.prototype.dimensions;
  108. function createOverlayCtor() {
  109. function Overlay(root) {
  110. this._root = root;
  111. }
  112. Overlay.prototype = new BMap.Overlay();
  113. /**
  114. * 初始化
  115. *
  116. * @param {BMap.Map} map
  117. * @override
  118. */
  119. Overlay.prototype.initialize = function (map) {
  120. map.getPanes().labelPane.appendChild(this._root);
  121. return this._root;
  122. };
  123. /**
  124. * @override
  125. */
  126. Overlay.prototype.draw = function () {};
  127. return Overlay;
  128. }
  129. BMapCoordSys.create = function (ecModel, api) {
  130. var bmapCoordSys;
  131. var root = api.getDom();
  132. // TODO Dispose
  133. ecModel.eachComponent('bmap', function (bmapModel) {
  134. var painter = api.getZr().painter;
  135. var viewportRoot = painter.getViewportRoot();
  136. if (typeof BMap === 'undefined') {
  137. throw new Error('BMap api is not loaded');
  138. }
  139. Overlay = Overlay || createOverlayCtor();
  140. if (bmapCoordSys) {
  141. throw new Error('Only one bmap component can exist');
  142. }
  143. if (!bmapModel.__bmap) {
  144. // Not support IE8
  145. var bmapRoot = root.querySelector('.ec-extension-bmap');
  146. if (bmapRoot) {
  147. // Reset viewport left and top, which will be changed
  148. // in moving handler in BMapView
  149. viewportRoot.style.left = '0px';
  150. viewportRoot.style.top = '0px';
  151. root.removeChild(bmapRoot);
  152. }
  153. bmapRoot = document.createElement('div');
  154. bmapRoot.style.cssText = 'width:100%;height:100%';
  155. // Not support IE8
  156. bmapRoot.classList.add('ec-extension-bmap');
  157. root.appendChild(bmapRoot);
  158. var bmap = bmapModel.__bmap = new BMap.Map(bmapRoot);
  159. var overlay = new Overlay(viewportRoot);
  160. bmap.addOverlay(overlay);
  161. // Override
  162. painter.getViewportRootOffset = function () {
  163. return {offsetLeft: 0, offsetTop: 0};
  164. };
  165. }
  166. var bmap = bmapModel.__bmap;
  167. // Set bmap options
  168. // centerAndZoom before layout and render
  169. var center = bmapModel.get('center');
  170. var zoom = bmapModel.get('zoom');
  171. if (center && zoom) {
  172. var pt = new BMap.Point(center[0], center[1]);
  173. bmap.centerAndZoom(pt, zoom);
  174. }
  175. bmapCoordSys = new BMapCoordSys(bmap, api);
  176. bmapCoordSys.setMapOffset(bmapModel.__mapOffset || [0, 0]);
  177. bmapCoordSys.setZoom(zoom);
  178. bmapCoordSys.setCenter(center);
  179. bmapModel.coordinateSystem = bmapCoordSys;
  180. });
  181. ecModel.eachSeries(function (seriesModel) {
  182. if (seriesModel.get('coordinateSystem') === 'bmap') {
  183. seriesModel.coordinateSystem = bmapCoordSys;
  184. }
  185. });
  186. };
  187. /*
  188. * Licensed to the Apache Software Foundation (ASF) under one
  189. * or more contributor license agreements. See the NOTICE file
  190. * distributed with this work for additional information
  191. * regarding copyright ownership. The ASF licenses this file
  192. * to you under the Apache License, Version 2.0 (the
  193. * "License"); you may not use this file except in compliance
  194. * with the License. You may obtain a copy of the License at
  195. *
  196. * http://www.apache.org/licenses/LICENSE-2.0
  197. *
  198. * Unless required by applicable law or agreed to in writing,
  199. * software distributed under the License is distributed on an
  200. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  201. * KIND, either express or implied. See the License for the
  202. * specific language governing permissions and limitations
  203. * under the License.
  204. */
  205. function v2Equal(a, b) {
  206. return a && b && a[0] === b[0] && a[1] === b[1];
  207. }
  208. echarts.extendComponentModel({
  209. type: 'bmap',
  210. getBMap: function () {
  211. // __bmap is injected when creating BMapCoordSys
  212. return this.__bmap;
  213. },
  214. setCenterAndZoom: function (center, zoom) {
  215. this.option.center = center;
  216. this.option.zoom = zoom;
  217. },
  218. centerOrZoomChanged: function (center, zoom) {
  219. var option = this.option;
  220. return !(v2Equal(center, option.center) && zoom === option.zoom);
  221. },
  222. defaultOption: {
  223. center: [104.114129, 37.550339],
  224. zoom: 5,
  225. mapStyle: {},
  226. mapStyleV2: {},
  227. roam: false
  228. }
  229. });
  230. /*
  231. * Licensed to the Apache Software Foundation (ASF) under one
  232. * or more contributor license agreements. See the NOTICE file
  233. * distributed with this work for additional information
  234. * regarding copyright ownership. The ASF licenses this file
  235. * to you under the Apache License, Version 2.0 (the
  236. * "License"); you may not use this file except in compliance
  237. * with the License. You may obtain a copy of the License at
  238. *
  239. * http://www.apache.org/licenses/LICENSE-2.0
  240. *
  241. * Unless required by applicable law or agreed to in writing,
  242. * software distributed under the License is distributed on an
  243. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  244. * KIND, either express or implied. See the License for the
  245. * specific language governing permissions and limitations
  246. * under the License.
  247. */
  248. echarts.extendComponentView({
  249. type: 'bmap',
  250. render: function (bMapModel, ecModel, api) {
  251. var rendering = true;
  252. var bmap = bMapModel.getBMap();
  253. var viewportRoot = api.getZr().painter.getViewportRoot();
  254. var coordSys = bMapModel.coordinateSystem;
  255. var moveHandler = function (type, target) {
  256. if (rendering) {
  257. return;
  258. }
  259. var offsetEl = viewportRoot.parentNode.parentNode.parentNode;
  260. var mapOffset = [
  261. -parseInt(offsetEl.style.left, 10) || 0,
  262. -parseInt(offsetEl.style.top, 10) || 0
  263. ];
  264. viewportRoot.style.left = mapOffset[0] + 'px';
  265. viewportRoot.style.top = mapOffset[1] + 'px';
  266. coordSys.setMapOffset(mapOffset);
  267. bMapModel.__mapOffset = mapOffset;
  268. api.dispatchAction({
  269. type: 'bmapRoam'
  270. });
  271. };
  272. function zoomEndHandler() {
  273. if (rendering) {
  274. return;
  275. }
  276. api.dispatchAction({
  277. type: 'bmapRoam'
  278. });
  279. }
  280. bmap.removeEventListener('moving', this._oldMoveHandler);
  281. // FIXME
  282. // Moveend may be triggered by centerAndZoom method when creating coordSys next time
  283. // bmap.removeEventListener('moveend', this._oldMoveHandler);
  284. bmap.removeEventListener('zoomend', this._oldZoomEndHandler);
  285. bmap.addEventListener('moving', moveHandler);
  286. // bmap.addEventListener('moveend', moveHandler);
  287. bmap.addEventListener('zoomend', zoomEndHandler);
  288. this._oldMoveHandler = moveHandler;
  289. this._oldZoomEndHandler = zoomEndHandler;
  290. var roam = bMapModel.get('roam');
  291. if (roam && roam !== 'scale') {
  292. bmap.enableDragging();
  293. }
  294. else {
  295. bmap.disableDragging();
  296. }
  297. if (roam && roam !== 'move') {
  298. bmap.enableScrollWheelZoom();
  299. bmap.enableDoubleClickZoom();
  300. bmap.enablePinchToZoom();
  301. }
  302. else {
  303. bmap.disableScrollWheelZoom();
  304. bmap.disableDoubleClickZoom();
  305. bmap.disablePinchToZoom();
  306. }
  307. /* map 2.0 */
  308. var originalStyle = bMapModel.__mapStyle;
  309. var newMapStyle = bMapModel.get('mapStyle') || {};
  310. // FIXME, Not use JSON methods
  311. var mapStyleStr = JSON.stringify(newMapStyle);
  312. if (JSON.stringify(originalStyle) !== mapStyleStr) {
  313. // FIXME May have blank tile when dragging if setMapStyle
  314. if (Object.keys(newMapStyle).length) {
  315. bmap.setMapStyle(newMapStyle);
  316. }
  317. bMapModel.__mapStyle = JSON.parse(mapStyleStr);
  318. }
  319. /* map 3.0 */
  320. var originalStyle2 = bMapModel.__mapStyle2;
  321. var newMapStyle2 = bMapModel.get('mapStyleV2') || {};
  322. // FIXME, Not use JSON methods
  323. var mapStyleStr2 = JSON.stringify(newMapStyle2);
  324. if (JSON.stringify(originalStyle2) !== mapStyleStr2) {
  325. // FIXME May have blank tile when dragging if setMapStyle
  326. if (Object.keys(newMapStyle2).length) {
  327. bmap.setMapStyleV2(newMapStyle2);
  328. }
  329. bMapModel.__mapStyle2 = JSON.parse(mapStyleStr2);
  330. }
  331. rendering = false;
  332. }
  333. });
  334. /*
  335. * Licensed to the Apache Software Foundation (ASF) under one
  336. * or more contributor license agreements. See the NOTICE file
  337. * distributed with this work for additional information
  338. * regarding copyright ownership. The ASF licenses this file
  339. * to you under the Apache License, Version 2.0 (the
  340. * "License"); you may not use this file except in compliance
  341. * with the License. You may obtain a copy of the License at
  342. *
  343. * http://www.apache.org/licenses/LICENSE-2.0
  344. *
  345. * Unless required by applicable law or agreed to in writing,
  346. * software distributed under the License is distributed on an
  347. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  348. * KIND, either express or implied. See the License for the
  349. * specific language governing permissions and limitations
  350. * under the License.
  351. */
  352. /**
  353. * BMap component extension
  354. */
  355. echarts.registerCoordinateSystem('bmap', BMapCoordSys);
  356. // Action
  357. echarts.registerAction({
  358. type: 'bmapRoam',
  359. event: 'bmapRoam',
  360. update: 'updateLayout'
  361. }, function (payload, ecModel) {
  362. ecModel.eachComponent('bmap', function (bMapModel) {
  363. var bmap = bMapModel.getBMap();
  364. var center = bmap.getCenter();
  365. bMapModel.setCenterAndZoom([center.lng, center.lat], bmap.getZoom());
  366. });
  367. });
  368. var version = '1.0.0';
  369. exports.version = version;
  370. })));
  371. //# sourceMappingURL=bmap.js.map