BMapView.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. import * as echarts from 'echarts';
  21. function isEmptyObject(obj) {
  22. for (const key in obj) {
  23. if (obj.hasOwnProperty(key)) {
  24. return false;
  25. }
  26. }
  27. return true;
  28. }
  29. export default echarts.extendComponentView({
  30. type: 'bmap',
  31. render: function (bMapModel, ecModel, api) {
  32. let rendering = true;
  33. const bmap = bMapModel.getBMap();
  34. const viewportRoot = api.getZr().painter.getViewportRoot();
  35. const coordSys = bMapModel.coordinateSystem;
  36. const moveHandler = function (type, target) {
  37. if (rendering) {
  38. return;
  39. }
  40. const offsetEl = viewportRoot.parentNode.parentNode.parentNode;
  41. const mapOffset = [
  42. -parseInt(offsetEl.style.left, 10) || 0,
  43. -parseInt(offsetEl.style.top, 10) || 0
  44. ];
  45. // only update style when map offset changed
  46. const viewportRootStyle = viewportRoot.style;
  47. const offsetLeft = mapOffset[0] + 'px';
  48. const offsetTop = mapOffset[1] + 'px';
  49. if (viewportRootStyle.left !== offsetLeft) {
  50. viewportRootStyle.left = offsetLeft;
  51. }
  52. if (viewportRootStyle.top !== offsetTop) {
  53. viewportRootStyle.top = offsetTop;
  54. }
  55. coordSys.setMapOffset(mapOffset);
  56. bMapModel.__mapOffset = mapOffset;
  57. api.dispatchAction({
  58. type: 'bmapRoam',
  59. animation: {
  60. duration: 0
  61. }
  62. });
  63. };
  64. function zoomEndHandler() {
  65. if (rendering) {
  66. return;
  67. }
  68. api.dispatchAction({
  69. type: 'bmapRoam',
  70. animation: {
  71. duration: 0
  72. }
  73. });
  74. }
  75. bmap.removeEventListener('moving', this._oldMoveHandler);
  76. bmap.removeEventListener('moveend', this._oldMoveHandler);
  77. bmap.removeEventListener('zoomend', this._oldZoomEndHandler);
  78. bmap.addEventListener('moving', moveHandler);
  79. bmap.addEventListener('moveend', moveHandler);
  80. bmap.addEventListener('zoomend', zoomEndHandler);
  81. this._oldMoveHandler = moveHandler;
  82. this._oldZoomEndHandler = zoomEndHandler;
  83. const roam = bMapModel.get('roam');
  84. if (roam && roam !== 'scale') {
  85. bmap.enableDragging();
  86. }
  87. else {
  88. bmap.disableDragging();
  89. }
  90. if (roam && roam !== 'move') {
  91. bmap.enableScrollWheelZoom();
  92. bmap.enableDoubleClickZoom();
  93. bmap.enablePinchToZoom();
  94. }
  95. else {
  96. bmap.disableScrollWheelZoom();
  97. bmap.disableDoubleClickZoom();
  98. bmap.disablePinchToZoom();
  99. }
  100. /* map 2.0 */
  101. const originalStyle = bMapModel.__mapStyle;
  102. const newMapStyle = bMapModel.get('mapStyle') || {};
  103. // FIXME, Not use JSON methods
  104. const mapStyleStr = JSON.stringify(newMapStyle);
  105. if (JSON.stringify(originalStyle) !== mapStyleStr) {
  106. // FIXME May have blank tile when dragging if setMapStyle
  107. if (!isEmptyObject(newMapStyle)) {
  108. bmap.setMapStyle(echarts.util.clone(newMapStyle));
  109. }
  110. bMapModel.__mapStyle = JSON.parse(mapStyleStr);
  111. }
  112. /* map 3.0 */
  113. const originalStyle2 = bMapModel.__mapStyle2;
  114. const newMapStyle2 = bMapModel.get('mapStyleV2') || {};
  115. // FIXME, Not use JSON methods
  116. const mapStyleStr2 = JSON.stringify(newMapStyle2);
  117. if (JSON.stringify(originalStyle2) !== mapStyleStr2) {
  118. // FIXME May have blank tile when dragging if setMapStyle
  119. if (!isEmptyObject(newMapStyle2)) {
  120. bmap.setMapStyleV2(echarts.util.clone(newMapStyle2));
  121. }
  122. bMapModel.__mapStyle2 = JSON.parse(mapStyleStr2);
  123. }
  124. rendering = false;
  125. }
  126. });