BMapCoordSys.js 7.4 KB

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