bmap.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. (function webpackUniversalModuleDefinition(root, factory) {
  2. if(typeof exports === 'object' && typeof module === 'object')
  3. module.exports = factory(require("echarts"));
  4. else if(typeof define === 'function' && define.amd)
  5. define(["echarts"], factory);
  6. else if(typeof exports === 'object')
  7. exports["bmap"] = factory(require("echarts"));
  8. else
  9. root["echarts"] = root["echarts"] || {}, root["echarts"]["bmap"] = factory(root["echarts"]);
  10. })(this, function(__WEBPACK_EXTERNAL_MODULE_1__) {
  11. return /******/ (function(modules) { // webpackBootstrap
  12. /******/ // The module cache
  13. /******/ var installedModules = {};
  14. /******/ // The require function
  15. /******/ function __webpack_require__(moduleId) {
  16. /******/ // Check if module is in cache
  17. /******/ if(installedModules[moduleId])
  18. /******/ return installedModules[moduleId].exports;
  19. /******/ // Create a new module (and put it into the cache)
  20. /******/ var module = installedModules[moduleId] = {
  21. /******/ exports: {},
  22. /******/ id: moduleId,
  23. /******/ loaded: false
  24. /******/ };
  25. /******/ // Execute the module function
  26. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  27. /******/ // Flag the module as loaded
  28. /******/ module.loaded = true;
  29. /******/ // Return the exports of the module
  30. /******/ return module.exports;
  31. /******/ }
  32. /******/ // expose the modules object (__webpack_modules__)
  33. /******/ __webpack_require__.m = modules;
  34. /******/ // expose the module cache
  35. /******/ __webpack_require__.c = installedModules;
  36. /******/ // __webpack_public_path__
  37. /******/ __webpack_require__.p = "";
  38. /******/ // Load entry module and return exports
  39. /******/ return __webpack_require__(0);
  40. /******/ })
  41. /************************************************************************/
  42. /******/ ([
  43. /* 0 */
  44. /***/ function(module, exports, __webpack_require__) {
  45. var __WEBPACK_AMD_DEFINE_RESULT__;/**
  46. * BMap component extension
  47. */
  48. !(__WEBPACK_AMD_DEFINE_RESULT__ = function (require) {
  49. __webpack_require__(1).registerCoordinateSystem(
  50. 'bmap', __webpack_require__(2)
  51. );
  52. __webpack_require__(3);
  53. __webpack_require__(4);
  54. // Action
  55. __webpack_require__(1).registerAction({
  56. type: 'bmapRoam',
  57. event: 'bmapRoam',
  58. update: 'updateLayout'
  59. }, function (payload, ecModel) {
  60. ecModel.eachComponent('bmap', function (bMapModel) {
  61. var bmap = bMapModel.getBMap();
  62. var center = bmap.getCenter();
  63. bMapModel.setCenterAndZoom([center.lng, center.lat], bmap.getZoom());
  64. });
  65. });
  66. return {
  67. version: '1.0.0'
  68. };
  69. }.call(exports, __webpack_require__, exports, module), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__));
  70. /***/ },
  71. /* 1 */
  72. /***/ function(module, exports) {
  73. module.exports = __WEBPACK_EXTERNAL_MODULE_1__;
  74. /***/ },
  75. /* 2 */
  76. /***/ function(module, exports, __webpack_require__) {
  77. var __WEBPACK_AMD_DEFINE_RESULT__;!(__WEBPACK_AMD_DEFINE_RESULT__ = function (require) {
  78. var echarts = __webpack_require__(1);
  79. function BMapCoordSys(bmap, api) {
  80. this._bmap = bmap;
  81. this.dimensions = ['lng', 'lat'];
  82. this._mapOffset = [0, 0];
  83. this._api = api;
  84. }
  85. BMapCoordSys.prototype.dimensions = ['lng', 'lat'];
  86. BMapCoordSys.prototype.setMapOffset = function (mapOffset) {
  87. this._mapOffset = mapOffset;
  88. };
  89. BMapCoordSys.prototype.getBMap = function () {
  90. return this._bmap;
  91. };
  92. BMapCoordSys.prototype.dataToPoint = function (data) {
  93. var point = new BMap.Point(data[0], data[1]);
  94. var px = this._bmap.pointToOverlayPixel(point);
  95. var mapOffset = this._mapOffset;
  96. return [px.x - mapOffset[0], px.y - mapOffset[1]];
  97. };
  98. BMapCoordSys.prototype.pointToData = function (pt) {
  99. var mapOffset = this._mapOffset;
  100. var pt = this._bmap.overlayPixelToPoint({
  101. x: pt[0] + mapOffset[0],
  102. y: pt[1] + mapOffset[1]
  103. });
  104. return [pt.lng, pt.lat];
  105. };
  106. BMapCoordSys.prototype.getViewRect = function () {
  107. var api = this._api;
  108. return new echarts.graphic.BoundingRect(0, 0, api.getWidth(), api.getHeight());
  109. };
  110. BMapCoordSys.prototype.getRoamTransform = function () {
  111. return echarts.matrix.create();
  112. };
  113. var Overlay;
  114. // For deciding which dimensions to use when creating list data
  115. BMapCoordSys.dimensions = BMapCoordSys.prototype.dimensions;
  116. function createOverlayCtor() {
  117. function Overlay(root) {
  118. this._root = root;
  119. }
  120. Overlay.prototype = new BMap.Overlay();
  121. /**
  122. * 初始化
  123. *
  124. * @param {BMap.Map} map
  125. * @override
  126. */
  127. Overlay.prototype.initialize = function (map) {
  128. map.getPanes().labelPane.appendChild(this._root);
  129. return this._root;
  130. };
  131. /**
  132. * @override
  133. */
  134. Overlay.prototype.draw = function () {};
  135. return Overlay;
  136. }
  137. BMapCoordSys.create = function (ecModel, api) {
  138. var bmapCoordSys;
  139. var root = api.getDom();
  140. // TODO Dispose
  141. ecModel.eachComponent('bmap', function (bmapModel) {
  142. var viewportRoot = api.getZr().painter.getViewportRoot();
  143. if (typeof BMap === 'undefined') {
  144. throw new Error('BMap api is not loaded');
  145. }
  146. Overlay = Overlay || createOverlayCtor();
  147. if (bmapCoordSys) {
  148. throw new Error('Only one bmap component can exist');
  149. }
  150. if (!bmapModel.__bmap) {
  151. // Not support IE8
  152. var bmapRoot = root.querySelector('.ec-extension-bmap');
  153. if (bmapRoot) {
  154. // Reset viewport left and top, which will be changed
  155. // in moving handler in BMapView
  156. viewportRoot.style.left = '0px';
  157. viewportRoot.style.top = '0px';
  158. root.removeChild(bmapRoot);
  159. }
  160. bmapRoot = document.createElement('div');
  161. bmapRoot.style.cssText = 'width:100%;height:100%';
  162. // Not support IE8
  163. bmapRoot.classList.add('ec-extension-bmap');
  164. root.appendChild(bmapRoot);
  165. var bmap = bmapModel.__bmap = new BMap.Map(bmapRoot);
  166. var overlay = new Overlay(viewportRoot);
  167. bmap.addOverlay(overlay);
  168. }
  169. var bmap = bmapModel.__bmap;
  170. // Set bmap options
  171. // centerAndZoom before layout and render
  172. var center = bmapModel.get('center');
  173. var zoom = bmapModel.get('zoom');
  174. if (center && zoom) {
  175. var pt = new BMap.Point(center[0], center[1]);
  176. bmap.centerAndZoom(pt, zoom);
  177. }
  178. bmapCoordSys = new BMapCoordSys(bmap, api);
  179. bmapCoordSys.setMapOffset(bmapModel.__mapOffset || [0, 0]);
  180. bmapModel.coordinateSystem = bmapCoordSys;
  181. });
  182. ecModel.eachSeries(function (seriesModel) {
  183. if (seriesModel.get('coordinateSystem') === 'bmap') {
  184. seriesModel.coordinateSystem = bmapCoordSys;
  185. }
  186. });
  187. };
  188. return BMapCoordSys;
  189. }.call(exports, __webpack_require__, exports, module), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__));
  190. /***/ },
  191. /* 3 */
  192. /***/ function(module, exports, __webpack_require__) {
  193. var __WEBPACK_AMD_DEFINE_RESULT__;!(__WEBPACK_AMD_DEFINE_RESULT__ = function (require) {
  194. function v2Equal(a, b) {
  195. return a && b && a[0] === b[0] && a[1] === b[1];
  196. }
  197. return __webpack_require__(1).extendComponentModel({
  198. type: 'bmap',
  199. getBMap: function () {
  200. // __bmap is injected when creating BMapCoordSys
  201. return this.__bmap;
  202. },
  203. setCenterAndZoom: function (center, zoom) {
  204. this.option.center = center;
  205. this.option.zoom = zoom;
  206. },
  207. centerOrZoomChanged: function (center, zoom) {
  208. var option = this.option;
  209. return !(v2Equal(center, option.center) && zoom === option.zoom);
  210. },
  211. defaultOption: {
  212. center: null,
  213. zoom: 1,
  214. mapStyle: {},
  215. roam: false
  216. }
  217. });
  218. }.call(exports, __webpack_require__, exports, module), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__));
  219. /***/ },
  220. /* 4 */
  221. /***/ function(module, exports, __webpack_require__) {
  222. var __WEBPACK_AMD_DEFINE_RESULT__;!(__WEBPACK_AMD_DEFINE_RESULT__ = function (require) {
  223. return __webpack_require__(1).extendComponentView({
  224. type: 'bmap',
  225. render: function (bMapModel, ecModel, api) {
  226. var rendering = true;
  227. var bmap = bMapModel.getBMap();
  228. var viewportRoot = api.getZr().painter.getViewportRoot();
  229. var coordSys = bMapModel.coordinateSystem;
  230. var moveHandler = function (type, target) {
  231. if (rendering) {
  232. return;
  233. }
  234. var offsetEl = viewportRoot.parentNode.parentNode.parentNode;
  235. var mapOffset = [
  236. -parseInt(offsetEl.style.left, 10) || 0,
  237. -parseInt(offsetEl.style.top, 10) || 0
  238. ];
  239. viewportRoot.style.left = mapOffset[0] + 'px';
  240. viewportRoot.style.top = mapOffset[1] + 'px';
  241. coordSys.setMapOffset(mapOffset);
  242. bMapModel.__mapOffset = mapOffset;
  243. api.dispatchAction({
  244. type: 'bmapRoam'
  245. });
  246. };
  247. function zoomEndHandler() {
  248. if (rendering) {
  249. return;
  250. }
  251. api.dispatchAction({
  252. type: 'bmapRoam'
  253. });
  254. }
  255. bmap.removeEventListener('moving', this._oldMoveHandler);
  256. // FIXME
  257. // Moveend may be triggered by centerAndZoom method when creating coordSys next time
  258. // bmap.removeEventListener('moveend', this._oldMoveHandler);
  259. bmap.removeEventListener('zoomend', this._oldZoomEndHandler);
  260. bmap.addEventListener('moving', moveHandler);
  261. // bmap.addEventListener('moveend', moveHandler);
  262. bmap.addEventListener('zoomend', zoomEndHandler);
  263. this._oldMoveHandler = moveHandler;
  264. this._oldZoomEndHandler = zoomEndHandler;
  265. var roam = bMapModel.get('roam');
  266. if (roam && roam !== 'scale') {
  267. bmap.enableDragging();
  268. }
  269. else {
  270. bmap.disableDragging();
  271. }
  272. if (roam && roam !== 'move') {
  273. bmap.enableScrollWheelZoom();
  274. bmap.enableDoubleClickZoom();
  275. bmap.enablePinchToZoom();
  276. }
  277. else {
  278. bmap.disableScrollWheelZoom();
  279. bmap.disableDoubleClickZoom();
  280. bmap.disablePinchToZoom();
  281. }
  282. var originalStyle = bMapModel.__mapStyle;
  283. var newMapStyle = bMapModel.get('mapStyle') || {};
  284. // FIXME, Not use JSON methods
  285. var mapStyleStr = JSON.stringify(newMapStyle);
  286. if (JSON.stringify(originalStyle) !== mapStyleStr) {
  287. bmap.setMapStyle(newMapStyle);
  288. bMapModel.__mapStyle = JSON.parse(mapStyleStr);
  289. }
  290. rendering = false;
  291. }
  292. });
  293. }.call(exports, __webpack_require__, exports, module), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__));
  294. /***/ }
  295. /******/ ])
  296. });
  297. ;