BMapView.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. /*
  21. * Licensed to the Apache Software Foundation (ASF) under one
  22. * or more contributor license agreements. See the NOTICE file
  23. * distributed with this work for additional information
  24. * regarding copyright ownership. The ASF licenses this file
  25. * to you under the Apache License, Version 2.0 (the
  26. * "License"); you may not use this file except in compliance
  27. * with the License. You may obtain a copy of the License at
  28. *
  29. * http://www.apache.org/licenses/LICENSE-2.0
  30. *
  31. * Unless required by applicable law or agreed to in writing,
  32. * software distributed under the License is distributed on an
  33. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  34. * KIND, either express or implied. See the License for the
  35. * specific language governing permissions and limitations
  36. * under the License.
  37. */
  38. function isEmptyObject(obj) {
  39. for (var key in obj) {
  40. if (obj.hasOwnProperty(key)) {
  41. return false;
  42. }
  43. }
  44. return true;
  45. }
  46. var _default = echarts.extendComponentView({
  47. type: 'bmap',
  48. render: function (bMapModel, ecModel, api) {
  49. var rendering = true;
  50. var bmap = bMapModel.getBMap();
  51. var viewportRoot = api.getZr().painter.getViewportRoot();
  52. var coordSys = bMapModel.coordinateSystem;
  53. var moveHandler = function (type, target) {
  54. if (rendering) {
  55. return;
  56. }
  57. var offsetEl = viewportRoot.parentNode.parentNode.parentNode;
  58. var mapOffset = [-parseInt(offsetEl.style.left, 10) || 0, -parseInt(offsetEl.style.top, 10) || 0];
  59. viewportRoot.style.left = mapOffset[0] + 'px';
  60. viewportRoot.style.top = mapOffset[1] + 'px';
  61. coordSys.setMapOffset(mapOffset);
  62. bMapModel.__mapOffset = mapOffset;
  63. api.dispatchAction({
  64. type: 'bmapRoam'
  65. });
  66. };
  67. function zoomEndHandler() {
  68. if (rendering) {
  69. return;
  70. }
  71. api.dispatchAction({
  72. type: 'bmapRoam'
  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. var roam = bMapModel.get('roam');
  84. if (roam && roam !== 'scale') {
  85. bmap.enableDragging();
  86. } else {
  87. bmap.disableDragging();
  88. }
  89. if (roam && roam !== 'move') {
  90. bmap.enableScrollWheelZoom();
  91. bmap.enableDoubleClickZoom();
  92. bmap.enablePinchToZoom();
  93. } else {
  94. bmap.disableScrollWheelZoom();
  95. bmap.disableDoubleClickZoom();
  96. bmap.disablePinchToZoom();
  97. }
  98. /* map 2.0 */
  99. var originalStyle = bMapModel.__mapStyle;
  100. var newMapStyle = bMapModel.get('mapStyle') || {}; // FIXME, Not use JSON methods
  101. var mapStyleStr = JSON.stringify(newMapStyle);
  102. if (JSON.stringify(originalStyle) !== mapStyleStr) {
  103. // FIXME May have blank tile when dragging if setMapStyle
  104. if (!isEmptyObject(newMapStyle2)) {
  105. bmap.setMapStyle(echarts.util.clone(newMapStyle));
  106. }
  107. bMapModel.__mapStyle = JSON.parse(mapStyleStr);
  108. }
  109. /* map 3.0 */
  110. var originalStyle2 = bMapModel.__mapStyle2;
  111. var newMapStyle2 = bMapModel.get('mapStyleV2') || {}; // FIXME, Not use JSON methods
  112. var mapStyleStr2 = JSON.stringify(newMapStyle2);
  113. if (JSON.stringify(originalStyle2) !== mapStyleStr2) {
  114. // FIXME May have blank tile when dragging if setMapStyle
  115. if (!isEmptyObject(newMapStyle2)) {
  116. bmap.setMapStyleV2(echarts.util.clone(newMapStyle2));
  117. }
  118. bMapModel.__mapStyle2 = JSON.parse(mapStyleStr2);
  119. }
  120. rendering = false;
  121. }
  122. });
  123. module.exports = _default;