BMapCoordSys.js 7.8 KB

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