BMapView.js 4.1 KB

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