BMapCoordSys.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. // @ts-nocheck
  20. /* global BMap */
  21. import {
  22. util as zrUtil,
  23. graphic,
  24. matrix
  25. } from 'echarts';
  26. function BMapCoordSys(bmap, api) {
  27. this._bmap = bmap;
  28. this.dimensions = ['lng', 'lat'];
  29. this._mapOffset = [0, 0];
  30. this._api = api;
  31. this._projection = new BMap.MercatorProjection();
  32. }
  33. BMapCoordSys.prototype.type = 'bmap';
  34. BMapCoordSys.prototype.dimensions = ['lng', 'lat'];
  35. BMapCoordSys.prototype.setZoom = function (zoom) {
  36. this._zoom = zoom;
  37. };
  38. BMapCoordSys.prototype.setCenter = function (center) {
  39. this._center = this._projection.lngLatToPoint(new BMap.Point(center[0], center[1]));
  40. };
  41. BMapCoordSys.prototype.setMapOffset = function (mapOffset) {
  42. this._mapOffset = mapOffset;
  43. };
  44. BMapCoordSys.prototype.getBMap = function () {
  45. return this._bmap;
  46. };
  47. BMapCoordSys.prototype.dataToPoint = function (data) {
  48. const point = new BMap.Point(data[0], data[1]);
  49. // TODO mercator projection is toooooooo slow
  50. // let mercatorPoint = this._projection.lngLatToPoint(point);
  51. // let width = this._api.getZr().getWidth();
  52. // let height = this._api.getZr().getHeight();
  53. // let divider = Math.pow(2, 18 - 10);
  54. // return [
  55. // Math.round((mercatorPoint.x - this._center.x) / divider + width / 2),
  56. // Math.round((this._center.y - mercatorPoint.y) / divider + height / 2)
  57. // ];
  58. const px = this._bmap.pointToOverlayPixel(point);
  59. const mapOffset = this._mapOffset;
  60. return [px.x - mapOffset[0], px.y - mapOffset[1]];
  61. };
  62. BMapCoordSys.prototype.pointToData = function (pt) {
  63. const mapOffset = this._mapOffset;
  64. pt = this._bmap.overlayPixelToPoint({
  65. x: pt[0] + mapOffset[0],
  66. y: pt[1] + mapOffset[1]
  67. });
  68. return [pt.lng, pt.lat];
  69. };
  70. BMapCoordSys.prototype.getViewRect = function () {
  71. const api = this._api;
  72. return new graphic.BoundingRect(0, 0, api.getWidth(), api.getHeight());
  73. };
  74. BMapCoordSys.prototype.getRoamTransform = function () {
  75. return matrix.create();
  76. };
  77. BMapCoordSys.prototype.prepareCustoms = function () {
  78. const rect = this.getViewRect();
  79. return {
  80. coordSys: {
  81. // The name exposed to user is always 'cartesian2d' but not 'grid'.
  82. type: 'bmap',
  83. x: rect.x,
  84. y: rect.y,
  85. width: rect.width,
  86. height: rect.height
  87. },
  88. api: {
  89. coord: zrUtil.bind(this.dataToPoint, this),
  90. size: zrUtil.bind(dataToCoordSize, this)
  91. }
  92. };
  93. };
  94. BMapCoordSys.prototype.convertToPixel = function (ecModel, finder, value) {
  95. // here we ignore finder as only one bmap component is allowed
  96. return this.dataToPoint(value);
  97. };
  98. BMapCoordSys.prototype.convertFromPixel = function (ecModel, finder, value) {
  99. return this.pointToData(value);
  100. };
  101. function dataToCoordSize(dataSize, dataItem) {
  102. dataItem = dataItem || [0, 0];
  103. return zrUtil.map([0, 1], function (dimIdx) {
  104. const val = dataItem[dimIdx];
  105. const halfSize = dataSize[dimIdx] / 2;
  106. const p1 = [];
  107. const p2 = [];
  108. p1[dimIdx] = val - halfSize;
  109. p2[dimIdx] = val + halfSize;
  110. p1[1 - dimIdx] = p2[1 - dimIdx] = dataItem[1 - dimIdx];
  111. return Math.abs(this.dataToPoint(p1)[dimIdx] - this.dataToPoint(p2)[dimIdx]);
  112. }, this);
  113. }
  114. let Overlay;
  115. // For deciding which dimensions to use when creating list data
  116. BMapCoordSys.dimensions = BMapCoordSys.prototype.dimensions;
  117. function createOverlayCtor() {
  118. function Overlay(root) {
  119. this._root = root;
  120. }
  121. Overlay.prototype = new BMap.Overlay();
  122. /**
  123. * 初始化
  124. *
  125. * @param {BMap.Map} map
  126. * @override
  127. */
  128. Overlay.prototype.initialize = function (map) {
  129. map.getPanes().labelPane.appendChild(this._root);
  130. return this._root;
  131. };
  132. /**
  133. * @override
  134. */
  135. Overlay.prototype.draw = function () {};
  136. return Overlay;
  137. }
  138. BMapCoordSys.create = function (ecModel, api) {
  139. let bmapCoordSys;
  140. const root = api.getDom();
  141. // TODO Dispose
  142. ecModel.eachComponent('bmap', function (bmapModel) {
  143. const painter = api.getZr().painter;
  144. const viewportRoot = painter.getViewportRoot();
  145. if (typeof BMap === 'undefined') {
  146. throw new Error('BMap api is not loaded');
  147. }
  148. Overlay = Overlay || createOverlayCtor();
  149. if (bmapCoordSys) {
  150. throw new Error('Only one bmap component can exist');
  151. }
  152. let bmap;
  153. if (!bmapModel.__bmap) {
  154. // Not support IE8
  155. let bmapRoot = root.querySelector('.ec-extension-bmap');
  156. if (bmapRoot) {
  157. // Reset viewport left and top, which will be changed
  158. // in moving handler in BMapView
  159. viewportRoot.style.left = '0px';
  160. viewportRoot.style.top = '0px';
  161. root.removeChild(bmapRoot);
  162. }
  163. bmapRoot = document.createElement('div');
  164. bmapRoot.className = 'ec-extension-bmap';
  165. // fix #13424
  166. bmapRoot.style.cssText = 'position:absolute;width:100%;height:100%';
  167. root.appendChild(bmapRoot);
  168. // initializes bmap
  169. let mapOptions = bmapModel.get('mapOptions');
  170. if (mapOptions) {
  171. mapOptions = zrUtil.clone(mapOptions);
  172. // Not support `mapType`, use `bmap.setMapType(MapType)` instead.
  173. delete mapOptions.mapType;
  174. }
  175. bmap = bmapModel.__bmap = new BMap.Map(bmapRoot, mapOptions);
  176. const overlay = new Overlay(viewportRoot);
  177. bmap.addOverlay(overlay);
  178. // Override
  179. painter.getViewportRootOffset = function () {
  180. return {offsetLeft: 0, offsetTop: 0};
  181. };
  182. }
  183. bmap = bmapModel.__bmap;
  184. // Set bmap options
  185. // centerAndZoom before layout and render
  186. const center = bmapModel.get('center');
  187. const zoom = bmapModel.get('zoom');
  188. if (center && zoom) {
  189. const bmapCenter = bmap.getCenter();
  190. const bmapZoom = bmap.getZoom();
  191. const centerOrZoomChanged = bmapModel.centerOrZoomChanged([bmapCenter.lng, bmapCenter.lat], bmapZoom);
  192. if (centerOrZoomChanged) {
  193. const pt = new BMap.Point(center[0], center[1]);
  194. bmap.centerAndZoom(pt, zoom);
  195. }
  196. }
  197. bmapCoordSys = new BMapCoordSys(bmap, api);
  198. bmapCoordSys.setMapOffset(bmapModel.__mapOffset || [0, 0]);
  199. bmapCoordSys.setZoom(zoom);
  200. bmapCoordSys.setCenter(center);
  201. bmapModel.coordinateSystem = bmapCoordSys;
  202. });
  203. ecModel.eachSeries(function (seriesModel) {
  204. if (seriesModel.get('coordinateSystem') === 'bmap') {
  205. seriesModel.coordinateSystem = bmapCoordSys;
  206. }
  207. });
  208. // return created coordinate systems
  209. return bmapCoordSys && [bmapCoordSys];
  210. };
  211. export default BMapCoordSys;