BMapView.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. define(function (require) {
  2. return require('echarts').extendComponentView({
  3. type: 'bmap',
  4. render: function (bMapModel, ecModel, api) {
  5. var rendering = true;
  6. var bmap = bMapModel.getBMap();
  7. var viewportRoot = api.getZr().painter.getViewportRoot();
  8. var coordSys = bMapModel.coordinateSystem;
  9. var moveHandler = function (type, target) {
  10. if (rendering) {
  11. return;
  12. }
  13. var offsetEl = viewportRoot.parentNode.parentNode.parentNode;
  14. var mapOffset = [
  15. -parseInt(offsetEl.style.left, 10) || 0,
  16. -parseInt(offsetEl.style.top, 10) || 0
  17. ];
  18. viewportRoot.style.left = mapOffset[0] + 'px';
  19. viewportRoot.style.top = mapOffset[1] + 'px';
  20. coordSys.setMapOffset(mapOffset);
  21. bMapModel.__mapOffset = mapOffset;
  22. api.dispatchAction({
  23. type: 'bmapRoam'
  24. });
  25. };
  26. function zoomEndHandler() {
  27. if (rendering) {
  28. return;
  29. }
  30. api.dispatchAction({
  31. type: 'bmapRoam'
  32. });
  33. }
  34. bmap.removeEventListener('moving', this._oldMoveHandler);
  35. // FIXME
  36. // Moveend may be triggered by centerAndZoom method when creating coordSys next time
  37. // bmap.removeEventListener('moveend', this._oldMoveHandler);
  38. bmap.removeEventListener('zoomend', this._oldZoomEndHandler);
  39. bmap.addEventListener('moving', moveHandler);
  40. // bmap.addEventListener('moveend', moveHandler);
  41. bmap.addEventListener('zoomend', zoomEndHandler);
  42. this._oldMoveHandler = moveHandler;
  43. this._oldZoomEndHandler = zoomEndHandler;
  44. var roam = bMapModel.get('roam');
  45. if (roam && roam !== 'scale') {
  46. bmap.enableDragging();
  47. }
  48. else {
  49. bmap.disableDragging();
  50. }
  51. if (roam && roam !== 'move') {
  52. bmap.enableScrollWheelZoom();
  53. bmap.enableDoubleClickZoom();
  54. bmap.enablePinchToZoom();
  55. }
  56. else {
  57. bmap.disableScrollWheelZoom();
  58. bmap.disableDoubleClickZoom();
  59. bmap.disablePinchToZoom();
  60. }
  61. var originalStyle = bMapModel.__mapStyle;
  62. var newMapStyle = bMapModel.get('mapStyle') || {};
  63. // FIXME, Not use JSON methods
  64. var mapStyleStr = JSON.stringify(newMapStyle);
  65. if (JSON.stringify(originalStyle) !== mapStyleStr) {
  66. bmap.setMapStyle(newMapStyle);
  67. bMapModel.__mapStyle = JSON.parse(mapStyleStr);
  68. }
  69. rendering = false;
  70. }
  71. });
  72. });