BMapCoordSys.js 8.3 KB

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