Marker.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import React, { Component, PureComponent } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { createMarker, updateMarker } from './api';
  4. var __com__ = 'Marker';
  5. //const debug = console.log;
  6. var debug = function debug() {};
  7. export var Marker = function (_Component) {
  8. babelHelpers.inherits(Marker, _Component);
  9. function Marker() {
  10. babelHelpers.classCallCheck(this, Marker);
  11. var _this = babelHelpers.possibleConstructorReturn(this, (Marker.__proto__ || Object.getPrototypeOf(Marker)).call(this));
  12. _this.refElement = null;
  13. _this._entity = null;
  14. debug(__com__, 'constructor', _this._entity);
  15. return _this;
  16. }
  17. babelHelpers.createClass(Marker, [{
  18. key: 'componentWillMount',
  19. value: function componentWillMount() {
  20. debug(__com__, 'componentWillMount', this.props.children, this._entity);
  21. }
  22. }, {
  23. key: 'componentDidMount',
  24. value: function componentDidMount() {
  25. debug(__com__, 'componentDidMount', this.props.children, this._entity);
  26. var _props = this.props,
  27. AMap = _props.AMap,
  28. __map__ = _props.__map__,
  29. options = _props.options,
  30. events = _props.events,
  31. children = _props.children;
  32. //let opts = { ...(options || {}), map: __map__, content: children };
  33. var opts = babelHelpers.extends({}, options || {}, { map: __map__ });
  34. this._entity = createMarker(AMap, opts, events);
  35. if (this._entity) {
  36. if (this.props.refer) this.props.refer(this._entity);
  37. }
  38. }
  39. }, {
  40. key: 'componentWillReceiveProps',
  41. value: function componentWillReceiveProps(nextProps) {
  42. debug(__com__, 'componentWillReceiveProps', this.props.children, this._entity);
  43. }
  44. }, {
  45. key: 'componentWillUpdate',
  46. value: function componentWillUpdate() {
  47. debug(__com__, 'componentWillUpdate', this.props.children, this._entity);
  48. }
  49. }, {
  50. key: 'componentDidUpdate',
  51. value: function componentDidUpdate(prevProps) {
  52. debug(__com__, 'componentDidUpdate', this.props.children, this._entity);
  53. var _props2 = this.props,
  54. AMap = _props2.AMap,
  55. __map__ = _props2.__map__,
  56. options = _props2.options,
  57. events = _props2.events,
  58. children = _props2.children;
  59. //let opts = { ...(options || {}), map: __map__, content: children };
  60. var opts = babelHelpers.extends({}, options || {}, { map: __map__ });
  61. if (!this._entity) {
  62. this._entity = createMarker(AMap, opts, events);
  63. if (this._entity) {
  64. if (this.props.refer) this.props.refer(this._entity);
  65. }
  66. return;
  67. }
  68. // need check props changes, then update.
  69. var oldOpts = babelHelpers.extends({}, prevProps.options || {}, {
  70. map: prevProps.__map__,
  71. content: prevProps.children
  72. });
  73. updateMarker(this._entity, opts, events, oldOpts, prevProps.events);
  74. }
  75. }, {
  76. key: 'componentWillUnmount',
  77. value: function componentWillUnmount() {
  78. debug(__com__, 'componentWillUnmount', this.props.children, this._entity);
  79. if (this._entity) {
  80. // this._entity.clearMap();
  81. this._entity.stopMove();
  82. this._entity.setMap(null);
  83. delete this._entity;
  84. // delete this._entity;
  85. this._entity = null;
  86. if (this.props.refer) this.props.refer(this._entity);
  87. }
  88. }
  89. // shouldComponentUpdate(nextProps, nextState) {
  90. // debug(__com__, 'shouldComponentUpdate', this._entity);
  91. // let { AMap: oldAMap, refer: oldRefer, options: oldOptions, events: oldEvents } = this.props;
  92. // let { AMap: newAMap, refer: newRefer, options: newOptions, events: newEvents } = nextProps;
  93. // if (oldAMap === newAMap && oldRefer === newRefer && oldOptions === newOptions && oldEvents === newEvents) {
  94. // debug(__com__, 'shouldComponentUpdate', false);
  95. // return false;
  96. // }
  97. // debug(__com__, 'shouldComponentUpdate', true);
  98. // return true;
  99. // }
  100. }, {
  101. key: 'render',
  102. value: function render() {
  103. debug(__com__, 'render', this.props.children, this._entity);
  104. var _props3 = this.props,
  105. AMap = _props3.AMap,
  106. options = _props3.options,
  107. events = _props3.events,
  108. match = _props3.match,
  109. location = _props3.location,
  110. history = _props3.history,
  111. staticContext = _props3.staticContext,
  112. rest = babelHelpers.objectWithoutProperties(_props3, ['AMap', 'options', 'events', 'match', 'location', 'history', 'staticContext']);
  113. return null;
  114. // return (
  115. // <React.Fragment>
  116. // </React.Fragment>
  117. // )
  118. }
  119. }]);
  120. return Marker;
  121. }(Component);
  122. Marker.propTypes = {
  123. AMap: PropTypes.object,
  124. __map__: PropTypes.object,
  125. options: PropTypes.object,
  126. events: PropTypes.object
  127. // zoom: PropTypes.number, // 10, //设置地图显示的缩放级别
  128. // center: PropTypes.array, // [116.397428, 39.90923],//设置地图中心点坐标
  129. // layers: PropTypes.array, // [new AMap.TileLayer.Satellite()], //设置图层,可设置成包含一个或多个图层的数组
  130. // mapStyle: PropTypes.string, // 'amap://styles/whitesmoke', //设置地图的显示样式
  131. // viewMode: PropTypes.string, // '2D', //设置地图模式
  132. // lang: PropTypes.string, // 'zh_cn', //设置地图语言类型
  133. // events: PropTypes.object, // {'click': function}, // 事件map
  134. };
  135. export default Marker;