BMapView.js 4.2 KB

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