BMapCoordSys.js 7.0 KB

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