BMapCoordSys.js 7.3 KB

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