BMapCoordSys.js 7.8 KB

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