BMapView.js 4.2 KB

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