bmap.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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.dimensions = ['lng', 'lat'];
  32. BMapCoordSys.prototype.setZoom = function (zoom) {
  33. this._zoom = zoom;
  34. };
  35. BMapCoordSys.prototype.setCenter = function (center) {
  36. this._center = this._projection.lngLatToPoint(new BMap.Point(center[0], center[1]));
  37. };
  38. BMapCoordSys.prototype.setMapOffset = function (mapOffset) {
  39. this._mapOffset = mapOffset;
  40. };
  41. BMapCoordSys.prototype.getBMap = function () {
  42. return this._bmap;
  43. };
  44. BMapCoordSys.prototype.dataToPoint = function (data) {
  45. var point = new BMap.Point(data[0], data[1]); // TODO mercator projection is toooooooo slow
  46. // let mercatorPoint = this._projection.lngLatToPoint(point);
  47. // let width = this._api.getZr().getWidth();
  48. // let height = this._api.getZr().getHeight();
  49. // let divider = Math.pow(2, 18 - 10);
  50. // return [
  51. // Math.round((mercatorPoint.x - this._center.x) / divider + width / 2),
  52. // Math.round((this._center.y - mercatorPoint.y) / divider + height / 2)
  53. // ];
  54. var px = this._bmap.pointToOverlayPixel(point);
  55. var mapOffset = this._mapOffset;
  56. return [px.x - mapOffset[0], px.y - mapOffset[1]];
  57. };
  58. BMapCoordSys.prototype.pointToData = function (pt) {
  59. var mapOffset = this._mapOffset;
  60. pt = this._bmap.overlayPixelToPoint({
  61. x: pt[0] + mapOffset[0],
  62. y: pt[1] + mapOffset[1]
  63. });
  64. return [pt.lng, pt.lat];
  65. };
  66. BMapCoordSys.prototype.getViewRect = function () {
  67. var api = this._api;
  68. return new echarts.graphic.BoundingRect(0, 0, api.getWidth(), api.getHeight());
  69. };
  70. BMapCoordSys.prototype.getRoamTransform = function () {
  71. return echarts.matrix.create();
  72. };
  73. BMapCoordSys.prototype.prepareCustoms = function () {
  74. var rect = this.getViewRect();
  75. return {
  76. coordSys: {
  77. // The name exposed to user is always 'cartesian2d' but not 'grid'.
  78. type: 'bmap',
  79. x: rect.x,
  80. y: rect.y,
  81. width: rect.width,
  82. height: rect.height
  83. },
  84. api: {
  85. coord: echarts.util.bind(this.dataToPoint, this),
  86. size: echarts.util.bind(dataToCoordSize, this)
  87. }
  88. };
  89. };
  90. function dataToCoordSize(dataSize, dataItem) {
  91. dataItem = dataItem || [0, 0];
  92. return echarts.util.map([0, 1], function (dimIdx) {
  93. var val = dataItem[dimIdx];
  94. var halfSize = dataSize[dimIdx] / 2;
  95. var p1 = [];
  96. var p2 = [];
  97. p1[dimIdx] = val - halfSize;
  98. p2[dimIdx] = val + halfSize;
  99. p1[1 - dimIdx] = p2[1 - dimIdx] = dataItem[1 - dimIdx];
  100. return Math.abs(this.dataToPoint(p1)[dimIdx] - this.dataToPoint(p2)[dimIdx]);
  101. }, this);
  102. }
  103. var Overlay; // For deciding which dimensions to use when creating list data
  104. BMapCoordSys.dimensions = BMapCoordSys.prototype.dimensions;
  105. function createOverlayCtor() {
  106. function Overlay(root) {
  107. this._root = root;
  108. }
  109. Overlay.prototype = new BMap.Overlay();
  110. /**
  111. * 初始化
  112. *
  113. * @param {BMap.Map} map
  114. * @override
  115. */
  116. Overlay.prototype.initialize = function (map) {
  117. map.getPanes().labelPane.appendChild(this._root);
  118. return this._root;
  119. };
  120. /**
  121. * @override
  122. */
  123. Overlay.prototype.draw = function () {};
  124. return Overlay;
  125. }
  126. BMapCoordSys.create = function (ecModel, api) {
  127. var bmapCoordSys;
  128. var root = api.getDom(); // TODO Dispose
  129. ecModel.eachComponent('bmap', function (bmapModel) {
  130. var painter = api.getZr().painter;
  131. var viewportRoot = painter.getViewportRoot();
  132. if (typeof BMap === 'undefined') {
  133. throw new Error('BMap api is not loaded');
  134. }
  135. Overlay = Overlay || createOverlayCtor();
  136. if (bmapCoordSys) {
  137. throw new Error('Only one bmap component can exist');
  138. }
  139. var bmap;
  140. if (!bmapModel.__bmap) {
  141. // Not support IE8
  142. var bmapRoot = root.querySelector('.ec-extension-bmap');
  143. if (bmapRoot) {
  144. // Reset viewport left and top, which will be changed
  145. // in moving handler in BMapView
  146. viewportRoot.style.left = '0px';
  147. viewportRoot.style.top = '0px';
  148. root.removeChild(bmapRoot);
  149. }
  150. bmapRoot = document.createElement('div');
  151. bmapRoot.className = 'ec-extension-bmap'; // fix #13424
  152. bmapRoot.style.cssText = 'position:absolute;width:100%;height:100%';
  153. root.appendChild(bmapRoot); // initializes bmap
  154. var mapOptions = bmapModel.get('mapOptions');
  155. if (mapOptions) {
  156. mapOptions = echarts.util.clone(mapOptions); // Not support `mapType`, use `bmap.setMapType(MapType)` instead.
  157. delete mapOptions.mapType;
  158. }
  159. bmap = bmapModel.__bmap = new BMap.Map(bmapRoot, mapOptions);
  160. var overlay = new Overlay(viewportRoot);
  161. bmap.addOverlay(overlay); // Override
  162. painter.getViewportRootOffset = function () {
  163. return {
  164. offsetLeft: 0,
  165. offsetTop: 0
  166. };
  167. };
  168. }
  169. bmap = bmapModel.__bmap; // Set bmap options
  170. // centerAndZoom before layout and render
  171. var center = bmapModel.get('center');
  172. var zoom = bmapModel.get('zoom');
  173. if (center && zoom) {
  174. var bmapCenter = bmap.getCenter();
  175. var bmapZoom = bmap.getZoom();
  176. var centerOrZoomChanged = bmapModel.centerOrZoomChanged([bmapCenter.lng, bmapCenter.lat], bmapZoom);
  177. if (centerOrZoomChanged) {
  178. var pt = new BMap.Point(center[0], center[1]);
  179. bmap.centerAndZoom(pt, zoom);
  180. }
  181. }
  182. bmapCoordSys = new BMapCoordSys(bmap, api);
  183. bmapCoordSys.setMapOffset(bmapModel.__mapOffset || [0, 0]);
  184. bmapCoordSys.setZoom(zoom);
  185. bmapCoordSys.setCenter(center);
  186. bmapModel.coordinateSystem = bmapCoordSys;
  187. });
  188. ecModel.eachSeries(function (seriesModel) {
  189. if (seriesModel.get('coordinateSystem') === 'bmap') {
  190. seriesModel.coordinateSystem = bmapCoordSys;
  191. }
  192. });
  193. };
  194. function v2Equal(a, b) {
  195. return a && b && a[0] === b[0] && a[1] === b[1];
  196. }
  197. echarts.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: [104.114129, 37.550339],
  213. zoom: 5,
  214. // 2.0 http://lbsyun.baidu.com/custom/index.htm
  215. mapStyle: {},
  216. // 3.0 http://lbsyun.baidu.com/index.php?title=open/custom
  217. mapStyleV2: {},
  218. // See https://lbsyun.baidu.com/cms/jsapi/reference/jsapi_reference.html#a0b1
  219. mapOptions: {},
  220. roam: false
  221. }
  222. });
  223. function isEmptyObject(obj) {
  224. for (var key in obj) {
  225. if (obj.hasOwnProperty(key)) {
  226. return false;
  227. }
  228. }
  229. return true;
  230. }
  231. echarts.extendComponentView({
  232. type: 'bmap',
  233. render: function (bMapModel, ecModel, api) {
  234. var rendering = true;
  235. var bmap = bMapModel.getBMap();
  236. var viewportRoot = api.getZr().painter.getViewportRoot();
  237. var coordSys = bMapModel.coordinateSystem;
  238. var moveHandler = function (type, target) {
  239. if (rendering) {
  240. return;
  241. }
  242. var offsetEl = viewportRoot.parentNode.parentNode.parentNode;
  243. var mapOffset = [-parseInt(offsetEl.style.left, 10) || 0, -parseInt(offsetEl.style.top, 10) || 0]; // only update style when map offset changed
  244. var viewportRootStyle = viewportRoot.style;
  245. var offsetLeft = mapOffset[0] + 'px';
  246. var offsetTop = mapOffset[1] + 'px';
  247. if (viewportRootStyle.left !== offsetLeft) {
  248. viewportRootStyle.left = offsetLeft;
  249. }
  250. if (viewportRootStyle.top !== offsetTop) {
  251. viewportRootStyle.top = offsetTop;
  252. }
  253. coordSys.setMapOffset(mapOffset);
  254. bMapModel.__mapOffset = mapOffset;
  255. api.dispatchAction({
  256. type: 'bmapRoam',
  257. animation: {
  258. duration: 0
  259. }
  260. });
  261. };
  262. function zoomEndHandler() {
  263. if (rendering) {
  264. return;
  265. }
  266. api.dispatchAction({
  267. type: 'bmapRoam',
  268. animation: {
  269. duration: 0
  270. }
  271. });
  272. }
  273. bmap.removeEventListener('moving', this._oldMoveHandler);
  274. bmap.removeEventListener('moveend', this._oldMoveHandler);
  275. bmap.removeEventListener('zoomend', this._oldZoomEndHandler);
  276. bmap.addEventListener('moving', moveHandler);
  277. bmap.addEventListener('moveend', moveHandler);
  278. bmap.addEventListener('zoomend', zoomEndHandler);
  279. this._oldMoveHandler = moveHandler;
  280. this._oldZoomEndHandler = zoomEndHandler;
  281. var roam = bMapModel.get('roam');
  282. if (roam && roam !== 'scale') {
  283. bmap.enableDragging();
  284. } else {
  285. bmap.disableDragging();
  286. }
  287. if (roam && roam !== 'move') {
  288. bmap.enableScrollWheelZoom();
  289. bmap.enableDoubleClickZoom();
  290. bmap.enablePinchToZoom();
  291. } else {
  292. bmap.disableScrollWheelZoom();
  293. bmap.disableDoubleClickZoom();
  294. bmap.disablePinchToZoom();
  295. }
  296. /* map 2.0 */
  297. var originalStyle = bMapModel.__mapStyle;
  298. var newMapStyle = bMapModel.get('mapStyle') || {}; // FIXME, Not use JSON methods
  299. var mapStyleStr = JSON.stringify(newMapStyle);
  300. if (JSON.stringify(originalStyle) !== mapStyleStr) {
  301. // FIXME May have blank tile when dragging if setMapStyle
  302. if (!isEmptyObject(newMapStyle)) {
  303. bmap.setMapStyle(echarts.util.clone(newMapStyle));
  304. }
  305. bMapModel.__mapStyle = JSON.parse(mapStyleStr);
  306. }
  307. /* map 3.0 */
  308. var originalStyle2 = bMapModel.__mapStyle2;
  309. var newMapStyle2 = bMapModel.get('mapStyleV2') || {}; // FIXME, Not use JSON methods
  310. var mapStyleStr2 = JSON.stringify(newMapStyle2);
  311. if (JSON.stringify(originalStyle2) !== mapStyleStr2) {
  312. // FIXME May have blank tile when dragging if setMapStyle
  313. if (!isEmptyObject(newMapStyle2)) {
  314. bmap.setMapStyleV2(echarts.util.clone(newMapStyle2));
  315. }
  316. bMapModel.__mapStyle2 = JSON.parse(mapStyleStr2);
  317. }
  318. rendering = false;
  319. }
  320. });
  321. echarts.registerCoordinateSystem('bmap', BMapCoordSys); // Action
  322. echarts.registerAction({
  323. type: 'bmapRoam',
  324. event: 'bmapRoam',
  325. update: 'updateLayout'
  326. }, function (payload, ecModel) {
  327. ecModel.eachComponent('bmap', function (bMapModel) {
  328. var bmap = bMapModel.getBMap();
  329. var center = bmap.getCenter();
  330. bMapModel.setCenterAndZoom([center.lng, center.lat], bmap.getZoom());
  331. });
  332. });
  333. var version = '1.0.0';
  334. exports.version = version;
  335. Object.defineProperty(exports, '__esModule', { value: true });
  336. })));
  337. //# sourceMappingURL=bmap.js.map