Marker.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  2. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  5. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  6. import React, { Component, PureComponent } from "react";
  7. import PropTypes from "prop-types";
  8. import { createMarker, updateMarker } from "./api";
  9. var __com__ = "Marker";
  10. //const debug = console.log;
  11. var debug = function debug() {};
  12. export var Marker = function (_Component) {
  13. _inherits(Marker, _Component);
  14. function Marker() {
  15. _classCallCheck(this, Marker);
  16. var _this = _possibleConstructorReturn(this, (Marker.__proto__ || Object.getPrototypeOf(Marker)).call(this));
  17. _this.refElement = null;
  18. _this._entity = null;
  19. debug(__com__, "constructor", _this._entity);
  20. return _this;
  21. }
  22. _createClass(Marker, [{
  23. key: "componentWillMount",
  24. value: function componentWillMount() {
  25. debug(__com__, "componentWillMount", this.props.children, this._entity);
  26. }
  27. }, {
  28. key: "componentDidMount",
  29. value: function componentDidMount() {
  30. debug(__com__, "componentDidMount", this.props.children, this._entity);
  31. var _props = this.props,
  32. __map__ = _props.__map__,
  33. options = _props.options,
  34. events = _props.events,
  35. children = _props.children;
  36. //let opts = { ...(options || {}), map: __map__, content: children };
  37. var opts = _extends({}, options || {}, { map: __map__ });
  38. this._entity = createMarker(opts, events);
  39. // if (this._entity) {
  40. // if (this.props.refer) this.props.refer(this._entity);
  41. // }
  42. }
  43. }, {
  44. key: "componentWillReceiveProps",
  45. value: function componentWillReceiveProps(nextProps) {
  46. debug(__com__, "componentWillReceiveProps", this.props.children, this._entity);
  47. }
  48. }, {
  49. key: "componentWillUpdate",
  50. value: function componentWillUpdate() {
  51. debug(__com__, "componentWillUpdate", this.props.children, this._entity);
  52. }
  53. }, {
  54. key: "componentDidUpdate",
  55. value: function componentDidUpdate(prevProps) {
  56. debug(__com__, "componentDidUpdate", this.props.children, this._entity);
  57. var _props2 = this.props,
  58. __map__ = _props2.__map__,
  59. options = _props2.options,
  60. events = _props2.events,
  61. children = _props2.children;
  62. //let opts = { ...(options || {}), map: __map__, content: children };
  63. var opts = _extends({}, options || {}, { map: __map__ });
  64. if (!this._entity) {
  65. this._entity = createMarker(opts, events);
  66. if (this._entity) {
  67. if (this.props.refer) this.props.refer(this._entity);
  68. }
  69. return;
  70. }
  71. // need check props changes, then update.
  72. var oldOpts = _extends({}, prevProps.options || {}, {
  73. map: prevProps.__map__,
  74. content: prevProps.children
  75. });
  76. updateMarker(this._entity, opts, events, oldOpts, prevProps.events);
  77. }
  78. }, {
  79. key: "componentWillUnmount",
  80. value: function componentWillUnmount() {
  81. debug(__com__, "componentWillUnmount", this.props.children, this._entity);
  82. if (this._entity) {
  83. // this._entity.clearMap();
  84. this._entity.stopMove();
  85. this._entity.setMap(null);
  86. this._entity = null;
  87. if (this.props.refer) this.props.refer(this._entity);
  88. }
  89. }
  90. // shouldComponentUpdate(nextProps, nextState) {
  91. // debug(__com__, 'shouldComponentUpdate', this._entity);
  92. // let { AMap: oldAMap, refer: oldRefer, options: oldOptions, events: oldEvents } = this.props;
  93. // let { AMap: newAMap, refer: newRefer, options: newOptions, events: newEvents } = nextProps;
  94. // if (oldAMap === newAMap && oldRefer === newRefer && oldOptions === newOptions && oldEvents === newEvents) {
  95. // debug(__com__, 'shouldComponentUpdate', false);
  96. // return false;
  97. // }
  98. // debug(__com__, 'shouldComponentUpdate', true);
  99. // return true;
  100. // }
  101. }, {
  102. key: "render",
  103. value: function render() {
  104. debug(__com__, "render", this._entity);
  105. return null;
  106. }
  107. }]);
  108. return Marker;
  109. }(Component);
  110. Marker.propTypes = {
  111. __map__: PropTypes.object,
  112. options: PropTypes.object,
  113. events: PropTypes.object
  114. // zoom: PropTypes.number, // 10, //设置地图显示的缩放级别
  115. // center: PropTypes.array, // [116.397428, 39.90923],//设置地图中心点坐标
  116. // layers: PropTypes.array, // [new AMap.TileLayer.Satellite()], //设置图层,可设置成包含一个或多个图层的数组
  117. // mapStyle: PropTypes.string, // 'amap://styles/whitesmoke', //设置地图的显示样式
  118. // viewMode: PropTypes.string, // '2D', //设置地图模式
  119. // lang: PropTypes.string, // 'zh_cn', //设置地图语言类型
  120. // events: PropTypes.object, // {'click': function}, // 事件map
  121. };
  122. export default Marker;