bmap.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('echarts')) :
  3. typeof define === 'function' && define.amd ? define(['exports', 'echarts'], factory) :
  4. (factory((global.bmap = {}),global.echarts));
  5. }(this, (function (exports,echarts) { 'use strict';
  6. /*
  7. * Licensed to the Apache Software Foundation (ASF) under one
  8. * or more contributor license agreements. See the NOTICE file
  9. * distributed with this work for additional information
  10. * regarding copyright ownership. The ASF licenses this file
  11. * to you under the Apache License, Version 2.0 (the
  12. * "License"); you may not use this file except in compliance
  13. * with the License. You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing,
  18. * software distributed under the License is distributed on an
  19. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  20. * KIND, either express or implied. See the License for the
  21. * specific language governing permissions and limitations
  22. * under the License.
  23. */
  24. /* global BMap */
  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 echarts.graphic.BoundingRect(0, 0, api.getWidth(), api.getHeight());
  71. };
  72. BMapCoordSys.prototype.getRoamTransform = function () {
  73. return echarts.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: echarts.util.bind(this.dataToPoint, this),
  88. size: echarts.util.bind(dataToCoordSize, this)
  89. }
  90. };
  91. };
  92. function dataToCoordSize(dataSize, dataItem) {
  93. dataItem = dataItem || [0, 0];
  94. return echarts.util.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. /*
  197. * Licensed to the Apache Software Foundation (ASF) under one
  198. * or more contributor license agreements. See the NOTICE file
  199. * distributed with this work for additional information
  200. * regarding copyright ownership. The ASF licenses this file
  201. * to you under the Apache License, Version 2.0 (the
  202. * "License"); you may not use this file except in compliance
  203. * with the License. You may obtain a copy of the License at
  204. *
  205. * http://www.apache.org/licenses/LICENSE-2.0
  206. *
  207. * Unless required by applicable law or agreed to in writing,
  208. * software distributed under the License is distributed on an
  209. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  210. * KIND, either express or implied. See the License for the
  211. * specific language governing permissions and limitations
  212. * under the License.
  213. */
  214. function v2Equal(a, b) {
  215. return a && b && a[0] === b[0] && a[1] === b[1];
  216. }
  217. echarts.extendComponentModel({
  218. type: 'bmap',
  219. getBMap: function () {
  220. // __bmap is injected when creating BMapCoordSys
  221. return this.__bmap;
  222. },
  223. setCenterAndZoom: function (center, zoom) {
  224. this.option.center = center;
  225. this.option.zoom = zoom;
  226. },
  227. centerOrZoomChanged: function (center, zoom) {
  228. var option = this.option;
  229. return !(v2Equal(center, option.center) && zoom === option.zoom);
  230. },
  231. defaultOption: {
  232. center: [104.114129, 37.550339],
  233. zoom: 5,
  234. // 2.0 http://lbsyun.baidu.com/custom/index.htm
  235. mapStyle: {},
  236. // 3.0 http://lbsyun.baidu.com/index.php?title=open/custom
  237. mapStyleV2: {},
  238. // See https://lbsyun.baidu.com/cms/jsapi/reference/jsapi_reference.html#a0b1
  239. mapOptions: {},
  240. roam: false
  241. }
  242. });
  243. /*
  244. * Licensed to the Apache Software Foundation (ASF) under one
  245. * or more contributor license agreements. See the NOTICE file
  246. * distributed with this work for additional information
  247. * regarding copyright ownership. The ASF licenses this file
  248. * to you under the Apache License, Version 2.0 (the
  249. * "License"); you may not use this file except in compliance
  250. * with the License. You may obtain a copy of the License at
  251. *
  252. * http://www.apache.org/licenses/LICENSE-2.0
  253. *
  254. * Unless required by applicable law or agreed to in writing,
  255. * software distributed under the License is distributed on an
  256. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  257. * KIND, either express or implied. See the License for the
  258. * specific language governing permissions and limitations
  259. * under the License.
  260. */
  261. function isEmptyObject(obj) {
  262. for (var key in obj) {
  263. if (obj.hasOwnProperty(key)) {
  264. return false;
  265. }
  266. }
  267. return true;
  268. }
  269. echarts.extendComponentView({
  270. type: 'bmap',
  271. render: function (bMapModel, ecModel, api) {
  272. var rendering = true;
  273. var bmap = bMapModel.getBMap();
  274. var viewportRoot = api.getZr().painter.getViewportRoot();
  275. var coordSys = bMapModel.coordinateSystem;
  276. var moveHandler = function (type, target) {
  277. if (rendering) {
  278. return;
  279. }
  280. var offsetEl = viewportRoot.parentNode.parentNode.parentNode;
  281. var mapOffset = [
  282. -parseInt(offsetEl.style.left, 10) || 0,
  283. -parseInt(offsetEl.style.top, 10) || 0
  284. ];
  285. viewportRoot.style.left = mapOffset[0] + 'px';
  286. viewportRoot.style.top = mapOffset[1] + 'px';
  287. coordSys.setMapOffset(mapOffset);
  288. bMapModel.__mapOffset = mapOffset;
  289. api.dispatchAction({
  290. type: 'bmapRoam'
  291. });
  292. };
  293. function zoomEndHandler() {
  294. if (rendering) {
  295. return;
  296. }
  297. api.dispatchAction({
  298. type: 'bmapRoam'
  299. });
  300. }
  301. bmap.removeEventListener('moving', this._oldMoveHandler);
  302. bmap.removeEventListener('moveend', this._oldMoveHandler);
  303. bmap.removeEventListener('zoomend', this._oldZoomEndHandler);
  304. bmap.addEventListener('moving', moveHandler);
  305. bmap.addEventListener('moveend', moveHandler);
  306. bmap.addEventListener('zoomend', zoomEndHandler);
  307. this._oldMoveHandler = moveHandler;
  308. this._oldZoomEndHandler = zoomEndHandler;
  309. var roam = bMapModel.get('roam');
  310. if (roam && roam !== 'scale') {
  311. bmap.enableDragging();
  312. }
  313. else {
  314. bmap.disableDragging();
  315. }
  316. if (roam && roam !== 'move') {
  317. bmap.enableScrollWheelZoom();
  318. bmap.enableDoubleClickZoom();
  319. bmap.enablePinchToZoom();
  320. }
  321. else {
  322. bmap.disableScrollWheelZoom();
  323. bmap.disableDoubleClickZoom();
  324. bmap.disablePinchToZoom();
  325. }
  326. /* map 2.0 */
  327. var originalStyle = bMapModel.__mapStyle;
  328. var newMapStyle = bMapModel.get('mapStyle') || {};
  329. // FIXME, Not use JSON methods
  330. var mapStyleStr = JSON.stringify(newMapStyle);
  331. if (JSON.stringify(originalStyle) !== mapStyleStr) {
  332. // FIXME May have blank tile when dragging if setMapStyle
  333. if (!isEmptyObject(newMapStyle2)) {
  334. bmap.setMapStyle(echarts.util.clone(newMapStyle));
  335. }
  336. bMapModel.__mapStyle = JSON.parse(mapStyleStr);
  337. }
  338. /* map 3.0 */
  339. var originalStyle2 = bMapModel.__mapStyle2;
  340. var newMapStyle2 = bMapModel.get('mapStyleV2') || {};
  341. // FIXME, Not use JSON methods
  342. var mapStyleStr2 = JSON.stringify(newMapStyle2);
  343. if (JSON.stringify(originalStyle2) !== mapStyleStr2) {
  344. // FIXME May have blank tile when dragging if setMapStyle
  345. if (!isEmptyObject(newMapStyle2)) {
  346. bmap.setMapStyleV2(echarts.util.clone(newMapStyle2));
  347. }
  348. bMapModel.__mapStyle2 = JSON.parse(mapStyleStr2);
  349. }
  350. rendering = false;
  351. }
  352. });
  353. /*
  354. * Licensed to the Apache Software Foundation (ASF) under one
  355. * or more contributor license agreements. See the NOTICE file
  356. * distributed with this work for additional information
  357. * regarding copyright ownership. The ASF licenses this file
  358. * to you under the Apache License, Version 2.0 (the
  359. * "License"); you may not use this file except in compliance
  360. * with the License. You may obtain a copy of the License at
  361. *
  362. * http://www.apache.org/licenses/LICENSE-2.0
  363. *
  364. * Unless required by applicable law or agreed to in writing,
  365. * software distributed under the License is distributed on an
  366. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  367. * KIND, either express or implied. See the License for the
  368. * specific language governing permissions and limitations
  369. * under the License.
  370. */
  371. /**
  372. * BMap component extension
  373. */
  374. echarts.registerCoordinateSystem('bmap', BMapCoordSys);
  375. // Action
  376. echarts.registerAction({
  377. type: 'bmapRoam',
  378. event: 'bmapRoam',
  379. update: 'updateLayout'
  380. }, function (payload, ecModel) {
  381. ecModel.eachComponent('bmap', function (bMapModel) {
  382. var bmap = bMapModel.getBMap();
  383. var center = bmap.getCenter();
  384. bMapModel.setCenterAndZoom([center.lng, center.lat], bmap.getZoom());
  385. });
  386. });
  387. var version = '1.0.0';
  388. exports.version = version;
  389. })));
  390. //# sourceMappingURL=bmap.js.map