bmap.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. roam: false
  227. }
  228. });
  229. /*
  230. * Licensed to the Apache Software Foundation (ASF) under one
  231. * or more contributor license agreements. See the NOTICE file
  232. * distributed with this work for additional information
  233. * regarding copyright ownership. The ASF licenses this file
  234. * to you under the Apache License, Version 2.0 (the
  235. * "License"); you may not use this file except in compliance
  236. * with the License. You may obtain a copy of the License at
  237. *
  238. * http://www.apache.org/licenses/LICENSE-2.0
  239. *
  240. * Unless required by applicable law or agreed to in writing,
  241. * software distributed under the License is distributed on an
  242. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  243. * KIND, either express or implied. See the License for the
  244. * specific language governing permissions and limitations
  245. * under the License.
  246. */
  247. echarts.extendComponentView({
  248. type: 'bmap',
  249. render: function (bMapModel, ecModel, api) {
  250. var rendering = true;
  251. var bmap = bMapModel.getBMap();
  252. var viewportRoot = api.getZr().painter.getViewportRoot();
  253. var coordSys = bMapModel.coordinateSystem;
  254. var moveHandler = function (type, target) {
  255. if (rendering) {
  256. return;
  257. }
  258. var offsetEl = viewportRoot.parentNode.parentNode.parentNode;
  259. var mapOffset = [
  260. -parseInt(offsetEl.style.left, 10) || 0,
  261. -parseInt(offsetEl.style.top, 10) || 0
  262. ];
  263. viewportRoot.style.left = mapOffset[0] + 'px';
  264. viewportRoot.style.top = mapOffset[1] + 'px';
  265. coordSys.setMapOffset(mapOffset);
  266. bMapModel.__mapOffset = mapOffset;
  267. api.dispatchAction({
  268. type: 'bmapRoam'
  269. });
  270. };
  271. function zoomEndHandler() {
  272. if (rendering) {
  273. return;
  274. }
  275. api.dispatchAction({
  276. type: 'bmapRoam'
  277. });
  278. }
  279. bmap.removeEventListener('moving', this._oldMoveHandler);
  280. // FIXME
  281. // Moveend may be triggered by centerAndZoom method when creating coordSys next time
  282. // bmap.removeEventListener('moveend', this._oldMoveHandler);
  283. bmap.removeEventListener('zoomend', this._oldZoomEndHandler);
  284. bmap.addEventListener('moving', moveHandler);
  285. // bmap.addEventListener('moveend', moveHandler);
  286. bmap.addEventListener('zoomend', zoomEndHandler);
  287. this._oldMoveHandler = moveHandler;
  288. this._oldZoomEndHandler = zoomEndHandler;
  289. var roam = bMapModel.get('roam');
  290. if (roam && roam !== 'scale') {
  291. bmap.enableDragging();
  292. }
  293. else {
  294. bmap.disableDragging();
  295. }
  296. if (roam && roam !== 'move') {
  297. bmap.enableScrollWheelZoom();
  298. bmap.enableDoubleClickZoom();
  299. bmap.enablePinchToZoom();
  300. }
  301. else {
  302. bmap.disableScrollWheelZoom();
  303. bmap.disableDoubleClickZoom();
  304. bmap.disablePinchToZoom();
  305. }
  306. var originalStyle = bMapModel.__mapStyle;
  307. var newMapStyle = bMapModel.get('mapStyle') || {};
  308. // FIXME, Not use JSON methods
  309. var mapStyleStr = JSON.stringify(newMapStyle);
  310. if (JSON.stringify(originalStyle) !== mapStyleStr) {
  311. // FIXME May have blank tile when dragging if setMapStyle
  312. if (Object.keys(newMapStyle).length) {
  313. bmap.setMapStyle(newMapStyle);
  314. }
  315. bMapModel.__mapStyle = JSON.parse(mapStyleStr);
  316. }
  317. rendering = false;
  318. }
  319. });
  320. /*
  321. * Licensed to the Apache Software Foundation (ASF) under one
  322. * or more contributor license agreements. See the NOTICE file
  323. * distributed with this work for additional information
  324. * regarding copyright ownership. The ASF licenses this file
  325. * to you under the Apache License, Version 2.0 (the
  326. * "License"); you may not use this file except in compliance
  327. * with the License. You may obtain a copy of the License at
  328. *
  329. * http://www.apache.org/licenses/LICENSE-2.0
  330. *
  331. * Unless required by applicable law or agreed to in writing,
  332. * software distributed under the License is distributed on an
  333. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  334. * KIND, either express or implied. See the License for the
  335. * specific language governing permissions and limitations
  336. * under the License.
  337. */
  338. /**
  339. * BMap component extension
  340. */
  341. echarts.registerCoordinateSystem('bmap', BMapCoordSys);
  342. // Action
  343. echarts.registerAction({
  344. type: 'bmapRoam',
  345. event: 'bmapRoam',
  346. update: 'updateLayout'
  347. }, function (payload, ecModel) {
  348. ecModel.eachComponent('bmap', function (bMapModel) {
  349. var bmap = bMapModel.getBMap();
  350. var center = bmap.getCenter();
  351. bMapModel.setCenterAndZoom([center.lng, center.lat], bmap.getZoom());
  352. });
  353. });
  354. var version = '1.0.0';
  355. exports.version = version;
  356. })));
  357. //# sourceMappingURL=bmap.js.map