Map.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import React, { Component, PureComponent } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { loadMap, createMap, updateMap } from './api';
  4. export class Map extends Component {
  5. static propTypes = {
  6. AMap: PropTypes.object,
  7. refMap: PropTypes.func, // 类似ref的函数形式,可以让父组件调用_entity
  8. options: PropTypes.object,
  9. events: PropTypes.object
  10. // zoom: PropTypes.number, // 10, //设置地图显示的缩放级别
  11. // center: PropTypes.array, // [116.397428, 39.90923],//设置地图中心点坐标
  12. // layers: PropTypes.array, // [new AMap.TileLayer.Satellite()], //设置图层,可设置成包含一个或多个图层的数组
  13. // mapStyle: PropTypes.string, // 'amap://styles/whitesmoke', //设置地图的显示样式
  14. // viewMode: PropTypes.string, // '2D', //设置地图模式
  15. // lang: PropTypes.string, // 'zh_cn', //设置地图语言类型
  16. // events: PropTypes.object, // {'click': function}, // 事件map
  17. };
  18. constructor() {
  19. super();
  20. this.refElement = null;
  21. this._entity = null;
  22. console.log('wcomponent constructor', this.refElement, this._entity);
  23. }
  24. componentWillMount() {
  25. console.log('wcomponentWillMount', this.refElement, this._entity);
  26. }
  27. componentDidMount() {
  28. console.log('wcomponentDidMount', this.refElement, this._entity);
  29. let { AMap, refMap, options, events } = this.props;
  30. this._entity = createMap(AMap, this.refElement, options, events);
  31. if (this._entity) {
  32. if (refMap) refMap(this._entity);
  33. this.setState({ __map__: this._entity });
  34. }
  35. }
  36. componentWillReceiveProps(nextProps) {
  37. console.log('wcomponentWillReceiveProps', this.refElement, this._entity);
  38. }
  39. componentWillUpdate() {
  40. console.log('wcomponentWillUpdate', this.refElement, this._entity);
  41. }
  42. componentDidUpdate(prevProps) {
  43. console.log('wcomponentDidUpdate', this.refElement, this._entity);
  44. let { AMap, refMap, options, events } = this.props;
  45. if (!this._entity) {
  46. this._entity = createMap(AMap, this.refElement, options, events);
  47. if (this._entity) {
  48. if (refMap) refMap(this._entity);
  49. this.setState({ __map__: this._entity });
  50. }
  51. return;
  52. }
  53. // need check props changes, then update.
  54. //updateMap(this._entity, this.props, prevProps);
  55. updateMap(
  56. this._entity,
  57. options,
  58. events,
  59. prevProps.options,
  60. prevProps.events
  61. );
  62. }
  63. componentWillUnmount() {
  64. console.log('wcomponentWillUnmount', this.refElement, this._entity);
  65. if (this._entity) {
  66. let { refMap } = this.props;
  67. // this._entity.clearMap();
  68. this._entity.destroy();
  69. // delete this._entity;
  70. this._entity = null;
  71. if (refMap) refMap(this._entity);
  72. }
  73. }
  74. renderChildren(children, __map__) {
  75. return React.Children.map(children, child => {
  76. if (child) {
  77. const cType = child.type;
  78. /* 针对下面两种组件不注入地图相关属性
  79. * 1. 明确声明不需要注入的
  80. * 2. DOM 元素
  81. */
  82. if (cType.preventAmap || typeof cType === 'string') {
  83. console.log('wcomponent renderChildren orig');
  84. return child;
  85. }
  86. console.log('wcomponent renderChildren add __map__');
  87. return React.cloneElement(child, {
  88. __map__
  89. });
  90. }
  91. console.log('wcomponent renderChildren null');
  92. return child;
  93. });
  94. }
  95. render() {
  96. console.log('wcomponent render', this.refElement, this._entity);
  97. let {
  98. AMap,
  99. options,
  100. match,
  101. location,
  102. history,
  103. children,
  104. staticContext,
  105. ...rest
  106. } = this.props;
  107. return (
  108. <div
  109. ref={ele => {
  110. this.refElement = ele;
  111. }}
  112. {...rest}
  113. >
  114. {this.renderChildren(children, this._entity)}
  115. </div>
  116. );
  117. }
  118. }
  119. export default Map;