bmap.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. (function (global, factory) {
  20. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('echarts')) :
  21. typeof define === 'function' && define.amd ? define(['exports', 'echarts'], factory) :
  22. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.bmap = {}, global.echarts));
  23. }(this, (function (exports, echarts) { 'use strict';
  24. function BMapCoordSys(bmap, api) {
  25. this._bmap = bmap;
  26. this.dimensions = ['lng', 'lat'];
  27. this._mapOffset = [0, 0];
  28. this._api = api;
  29. this._projection = new BMap.MercatorProjection();
  30. }
  31. BMapCoordSys.prototype.type = 'bmap';
  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. // let mercatorPoint = this._projection.lngLatToPoint(point);
  49. // let width = this._api.getZr().getWidth();
  50. // let height = this._api.getZr().getHeight();
  51. // let 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. 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 () {
  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. BMapCoordSys.prototype.convertToPixel = function (ecModel, finder, value) {
  93. // here we ignore finder as only one bmap component is allowed
  94. return this.dataToPoint(value);
  95. };
  96. BMapCoordSys.prototype.convertFromPixel = function (ecModel, finder, value) {
  97. return this.pointToData(value);
  98. };
  99. function dataToCoordSize(dataSize, dataItem) {
  100. dataItem = dataItem || [0, 0];
  101. return echarts.util.map([0, 1], function (dimIdx) {
  102. var val = dataItem[dimIdx];
  103. var halfSize = dataSize[dimIdx] / 2;
  104. var p1 = [];
  105. var p2 = [];
  106. p1[dimIdx] = val - halfSize;
  107. p2[dimIdx] = val + halfSize;
  108. p1[1 - dimIdx] = p2[1 - dimIdx] = dataItem[1 - dimIdx];
  109. return Math.abs(this.dataToPoint(p1)[dimIdx] - this.dataToPoint(p2)[dimIdx]);
  110. }, this);
  111. }
  112. var Overlay;
  113. // For deciding which dimensions to use when creating list data
  114. BMapCoordSys.dimensions = BMapCoordSys.prototype.dimensions;
  115. function createOverlayCtor() {
  116. function Overlay(root) {
  117. this._root = root;
  118. }
  119. Overlay.prototype = new BMap.Overlay();
  120. /**
  121. * 初始化
  122. *
  123. * @param {BMap.Map} map
  124. * @override
  125. */
  126. Overlay.prototype.initialize = function (map) {
  127. map.getPanes().labelPane.appendChild(this._root);
  128. return this._root;
  129. };
  130. /**
  131. * @override
  132. */
  133. Overlay.prototype.draw = function () {};
  134. return Overlay;
  135. }
  136. BMapCoordSys.create = function (ecModel, api) {
  137. var bmapCoordSys;
  138. var root = api.getDom();
  139. // TODO Dispose
  140. ecModel.eachComponent('bmap', function (bmapModel) {
  141. var painter = api.getZr().painter;
  142. var viewportRoot = 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. var bmap;
  151. if (!bmapModel.__bmap) {
  152. // Not support IE8
  153. var bmapRoot = root.querySelector('.ec-extension-bmap');
  154. if (bmapRoot) {
  155. // Reset viewport left and top, which will be changed
  156. // in moving handler in BMapView
  157. viewportRoot.style.left = '0px';
  158. viewportRoot.style.top = '0px';
  159. root.removeChild(bmapRoot);
  160. }
  161. bmapRoot = document.createElement('div');
  162. bmapRoot.className = 'ec-extension-bmap';
  163. // fix #13424
  164. bmapRoot.style.cssText = 'position:absolute;width:100%;height:100%';
  165. root.appendChild(bmapRoot);
  166. // initializes bmap
  167. var mapOptions = bmapModel.get('mapOptions');
  168. if (mapOptions) {
  169. mapOptions = echarts.util.clone(mapOptions);
  170. // Not support `mapType`, use `bmap.setMapType(MapType)` instead.
  171. delete mapOptions.mapType;
  172. }
  173. bmap = bmapModel.__bmap = new BMap.Map(bmapRoot, mapOptions);
  174. var overlay = new Overlay(viewportRoot);
  175. bmap.addOverlay(overlay);
  176. // Override
  177. painter.getViewportRootOffset = function () {
  178. return {
  179. offsetLeft: 0,
  180. offsetTop: 0
  181. };
  182. };
  183. }
  184. bmap = bmapModel.__bmap;
  185. // Set bmap options
  186. // centerAndZoom before layout and render
  187. var center = bmapModel.get('center');
  188. var zoom = bmapModel.get('zoom');
  189. if (center && zoom) {
  190. var bmapCenter = bmap.getCenter();
  191. var bmapZoom = bmap.getZoom();
  192. var centerOrZoomChanged = bmapModel.centerOrZoomChanged([bmapCenter.lng, bmapCenter.lat], bmapZoom);
  193. if (centerOrZoomChanged) {
  194. var pt = new BMap.Point(center[0], center[1]);
  195. bmap.centerAndZoom(pt, zoom);
  196. }
  197. }
  198. bmapCoordSys = new BMapCoordSys(bmap, api);
  199. bmapCoordSys.setMapOffset(bmapModel.__mapOffset || [0, 0]);
  200. bmapCoordSys.setZoom(zoom);
  201. bmapCoordSys.setCenter(center);
  202. bmapModel.coordinateSystem = bmapCoordSys;
  203. });
  204. ecModel.eachSeries(function (seriesModel) {
  205. if (seriesModel.get('coordinateSystem') === 'bmap') {
  206. seriesModel.coordinateSystem = bmapCoordSys;
  207. }
  208. });
  209. // return created coordinate systems
  210. return bmapCoordSys && [bmapCoordSys];
  211. };
  212. function v2Equal(a, b) {
  213. return a && b && a[0] === b[0] && a[1] === b[1];
  214. }
  215. echarts.extendComponentModel({
  216. type: 'bmap',
  217. getBMap: function () {
  218. // __bmap is injected when creating BMapCoordSys
  219. return this.__bmap;
  220. },
  221. setCenterAndZoom: function (center, zoom) {
  222. this.option.center = center;
  223. this.option.zoom = zoom;
  224. },
  225. centerOrZoomChanged: function (center, zoom) {
  226. var option = this.option;
  227. return !(v2Equal(center, option.center) && zoom === option.zoom);
  228. },
  229. defaultOption: {
  230. center: [104.114129, 37.550339],
  231. zoom: 5,
  232. // 2.0 https://lbsyun.baidu.com/custom/index.htm
  233. mapStyle: {},
  234. // 3.0 https://lbsyun.baidu.com/index.php?title=open/custom
  235. mapStyleV2: {},
  236. // See https://lbsyun.baidu.com/cms/jsapi/reference/jsapi_reference.html#a0b1
  237. mapOptions: {},
  238. roam: false
  239. }
  240. });
  241. function isEmptyObject(obj) {
  242. for (var key in obj) {
  243. if (obj.hasOwnProperty(key)) {
  244. return false;
  245. }
  246. }
  247. return true;
  248. }
  249. echarts.extendComponentView({
  250. type: 'bmap',
  251. render: function (bMapModel, ecModel, api) {
  252. var rendering = true;
  253. var bmap = bMapModel.getBMap();
  254. var viewportRoot = api.getZr().painter.getViewportRoot();
  255. var coordSys = bMapModel.coordinateSystem;
  256. var moveHandler = function (type, target) {
  257. if (rendering) {
  258. return;
  259. }
  260. var offsetEl = viewportRoot.parentNode.parentNode.parentNode;
  261. var mapOffset = [-parseInt(offsetEl.style.left, 10) || 0, -parseInt(offsetEl.style.top, 10) || 0];
  262. // only update style when map offset changed
  263. var viewportRootStyle = viewportRoot.style;
  264. var offsetLeft = mapOffset[0] + 'px';
  265. var offsetTop = mapOffset[1] + 'px';
  266. if (viewportRootStyle.left !== offsetLeft) {
  267. viewportRootStyle.left = offsetLeft;
  268. }
  269. if (viewportRootStyle.top !== offsetTop) {
  270. viewportRootStyle.top = offsetTop;
  271. }
  272. coordSys.setMapOffset(mapOffset);
  273. bMapModel.__mapOffset = mapOffset;
  274. api.dispatchAction({
  275. type: 'bmapRoam',
  276. animation: {
  277. duration: 0
  278. }
  279. });
  280. };
  281. function zoomEndHandler() {
  282. if (rendering) {
  283. return;
  284. }
  285. api.dispatchAction({
  286. type: 'bmapRoam',
  287. animation: {
  288. duration: 0
  289. }
  290. });
  291. }
  292. bmap.removeEventListener('moving', this._oldMoveHandler);
  293. bmap.removeEventListener('moveend', this._oldMoveHandler);
  294. bmap.removeEventListener('zoomend', this._oldZoomEndHandler);
  295. bmap.addEventListener('moving', moveHandler);
  296. bmap.addEventListener('moveend', moveHandler);
  297. bmap.addEventListener('zoomend', zoomEndHandler);
  298. this._oldMoveHandler = moveHandler;
  299. this._oldZoomEndHandler = zoomEndHandler;
  300. var roam = bMapModel.get('roam');
  301. if (roam && roam !== 'scale') {
  302. bmap.enableDragging();
  303. } else {
  304. bmap.disableDragging();
  305. }
  306. if (roam && roam !== 'move') {
  307. bmap.enableScrollWheelZoom();
  308. bmap.enableDoubleClickZoom();
  309. bmap.enablePinchToZoom();
  310. } else {
  311. bmap.disableScrollWheelZoom();
  312. bmap.disableDoubleClickZoom();
  313. bmap.disablePinchToZoom();
  314. }
  315. /* map 2.0 */
  316. var originalStyle = bMapModel.__mapStyle;
  317. var newMapStyle = bMapModel.get('mapStyle') || {};
  318. // FIXME, Not use JSON methods
  319. var mapStyleStr = JSON.stringify(newMapStyle);
  320. if (JSON.stringify(originalStyle) !== mapStyleStr) {
  321. // FIXME May have blank tile when dragging if setMapStyle
  322. if (!isEmptyObject(newMapStyle)) {
  323. bmap.setMapStyle(echarts.util.clone(newMapStyle));
  324. }
  325. bMapModel.__mapStyle = JSON.parse(mapStyleStr);
  326. }
  327. /* map 3.0 */
  328. var originalStyle2 = bMapModel.__mapStyle2;
  329. var newMapStyle2 = bMapModel.get('mapStyleV2') || {};
  330. // FIXME, Not use JSON methods
  331. var mapStyleStr2 = JSON.stringify(newMapStyle2);
  332. if (JSON.stringify(originalStyle2) !== mapStyleStr2) {
  333. // FIXME May have blank tile when dragging if setMapStyle
  334. if (!isEmptyObject(newMapStyle2)) {
  335. bmap.setMapStyleV2(echarts.util.clone(newMapStyle2));
  336. }
  337. bMapModel.__mapStyle2 = JSON.parse(mapStyleStr2);
  338. }
  339. rendering = false;
  340. }
  341. });
  342. echarts.registerCoordinateSystem('bmap', BMapCoordSys);
  343. // Action
  344. echarts.registerAction({
  345. type: 'bmapRoam',
  346. event: 'bmapRoam',
  347. update: 'updateLayout'
  348. }, function (payload, ecModel) {
  349. ecModel.eachComponent('bmap', function (bMapModel) {
  350. var bmap = bMapModel.getBMap();
  351. var center = bmap.getCenter();
  352. bMapModel.setCenterAndZoom([center.lng, center.lat], bmap.getZoom());
  353. });
  354. });
  355. var version = '1.0.0';
  356. exports.version = version;
  357. Object.defineProperty(exports, '__esModule', { value: true });
  358. })));
  359. //# sourceMappingURL=bmap.js.map