BMapCoordSys.ts 7.2 KB

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