Marker.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import React, { Component, PureComponent } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { createMarker, updateMarker } from './api';
  4. const __com__ = 'Marker';
  5. //const debug = console.log;
  6. const debug = () => {};
  7. export class Marker extends Component {
  8. static propTypes = {
  9. __map__: PropTypes.object,
  10. options: PropTypes.object,
  11. events: PropTypes.object
  12. // zoom: PropTypes.number, // 10, //设置地图显示的缩放级别
  13. // center: PropTypes.array, // [116.397428, 39.90923],//设置地图中心点坐标
  14. // layers: PropTypes.array, // [new AMap.TileLayer.Satellite()], //设置图层,可设置成包含一个或多个图层的数组
  15. // mapStyle: PropTypes.string, // 'amap://styles/whitesmoke', //设置地图的显示样式
  16. // viewMode: PropTypes.string, // '2D', //设置地图模式
  17. // lang: PropTypes.string, // 'zh_cn', //设置地图语言类型
  18. // events: PropTypes.object, // {'click': function}, // 事件map
  19. };
  20. constructor() {
  21. super();
  22. this.refElement = null;
  23. this._entity = null;
  24. debug(__com__, 'constructor', this._entity);
  25. }
  26. componentWillMount() {
  27. debug(__com__, 'componentWillMount', this.props.children, this._entity);
  28. }
  29. componentDidMount() {
  30. debug(__com__, 'componentDidMount', this.props.children, this._entity);
  31. let { __map__, options, events, children } = this.props;
  32. //let opts = { ...(options || {}), map: __map__, content: children };
  33. let opts = { ...(options || {}), map: __map__ };
  34. this._entity = createMarker(opts, events);
  35. if (this._entity) {
  36. if (this.props.refer) this.props.refer(this._entity);
  37. }
  38. }
  39. componentWillReceiveProps(nextProps) {
  40. debug(__com__, 'componentWillReceiveProps', this.props.children, this._entity);
  41. }
  42. componentWillUpdate() {
  43. debug(__com__, 'componentWillUpdate', this.props.children, this._entity);
  44. }
  45. componentDidUpdate(prevProps) {
  46. debug(__com__, 'componentDidUpdate', this.props.children, this._entity);
  47. let { __map__, options, events, children } = this.props;
  48. //let opts = { ...(options || {}), map: __map__, content: children };
  49. let opts = { ...(options || {}), map: __map__ };
  50. if (!this._entity) {
  51. this._entity = createMarker(opts, events);
  52. if (this._entity) {
  53. if (this.props.refer) this.props.refer(this._entity);
  54. }
  55. return;
  56. }
  57. // need check props changes, then update.
  58. let oldOpts = {
  59. ...(prevProps.options || {}),
  60. map: prevProps.__map__,
  61. content: prevProps.children
  62. };
  63. updateMarker(this._entity, opts, events, oldOpts, prevProps.events);
  64. }
  65. componentWillUnmount() {
  66. debug(__com__, 'componentWillUnmount', this.props.children, this._entity);
  67. if (this._entity) {
  68. // this._entity.clearMap();
  69. this._entity.stopMove();
  70. this._entity.setMap(null);
  71. this._entity = null;
  72. if (this.props.refer) this.props.refer(this._entity);
  73. }
  74. }
  75. // shouldComponentUpdate(nextProps, nextState) {
  76. // debug(__com__, 'shouldComponentUpdate', this._entity);
  77. // let { AMap: oldAMap, refer: oldRefer, options: oldOptions, events: oldEvents } = this.props;
  78. // let { AMap: newAMap, refer: newRefer, options: newOptions, events: newEvents } = nextProps;
  79. // if (oldAMap === newAMap && oldRefer === newRefer && oldOptions === newOptions && oldEvents === newEvents) {
  80. // debug(__com__, 'shouldComponentUpdate', false);
  81. // return false;
  82. // }
  83. // debug(__com__, 'shouldComponentUpdate', true);
  84. // return true;
  85. // }
  86. render() {
  87. debug(__com__, 'render', this._entity);
  88. return null;
  89. }
  90. }
  91. export default Marker;