import React from 'react'; import ReactDOM from 'react-dom'; import isEmpty from 'lodash/isEmpty'; import {Icon} from 'antd'; import { WeaMap } from 'ecCom'; import { Polyline, Marker, InfoWindow } from 'react-amap'; import '../style/map.less'; let mapInstance; const iconCls = { '0': 'markerRBg', '1': 'markerYBg' } const colors = ["#0072E3", "#BE77FF", "#FFD306", "#02DF82", "#D94600", "#AE0000", "#5CADAD", "#AE57A4", "#613030 ", "#D9006C"]; const cLength = colors.length; const content = i => `
${i + 1}
`; const createDetailTemplete = c => { const {crm = [], pics = []} = c; let picDom = '',crmDom = ''; pics.map(pic => picDom += `
`); crm.map(d => crmDom += `
${d}
`); return `
${c.name} ${c.time} ${c.signTitle}
${c.information?`
${c.information}
`:''} ${c.pics?`
${picDom}
`:''} ${crm && crm.length > 0 ? `
${crmDom}
` : ''}
${c.title}
` } class Main extends React.Component { constructor(props) { super(props); this.state = { markers: [], polylines: [], infoWindow: { visible: false, isCustom: true }, showMap: false } } componentDidMount() { window.closeHrmMapInfoWindow = this.closeInfoWindow; this.initMap(); } componentWillReceiveProps(nextProps){ this.initMap(nextProps); } closeInfoWindow = () => { this.setState({ infoWindow: { ...this.state.infoWindow, visible: false } }) } initMap = (nextProps) => { this.setState({showMap: false}); let _props = nextProps || this.props; const {config, mult} = _props; if(isEmpty(config) || (mult && config.length == 0)) return; this.setState({infoWindow: {...this.state.infoWindow, visible: false}}); this.addMarker(_props); } addMarker = (nextProps) => { let _props = nextProps || this.props; const { config, mult } = _props let markers = []; let polylines = []; if(mult){ config.map((c, i) => { let path = []; c.items.reverse().map((item, j) => { const markerProps = { title: item.title, content: content(j), position: { longitude: item.x, latitude: item.y }, events: { click: (e) => { this.setState({ infoWindow: { ...this.state.infoWindow, content:createDetailTemplete(item), position: { longitude: item.x, latitude: item.y }, visible: true } }) } } } path.push(markerProps.position); markers.push() }); const polylineProps = { path, style: { strokeColor: colors[i % cLength], strokeOpacity: 1, strokeWeight: 2, strokeStyle: "solid" } } polylines.push() }) }else{ const markerProps = { title: config.title, content: content(0), position: { longitude: config.x, latitude: config.y }, events: { click: (e) => { this.setState({ infoWindow: { ...this.state.infoWindow, content:createDetailTemplete(config), position: { longitude: config.x, latitude: config.y }, visible: true } }) } } } markers.push() } this.setState({markers, polylines, showMap: true}); } setFitView = () => { setTimeout(() => { mapInstance && mapInstance.setFitView(); }, 0) } setMapInstance = (instanace) => { if(instanace.CLASS_NAME === 'AMap.Map'){ mapInstance = instanace; } } render() { const mapEvt = { created: instanace => this.setMapInstance(instanace), complete: () => this.setFitView() } const mapProps = { zoom: 14, resizeEnable: true, plugins: [ 'Scale', { name: 'ToolBar', options: { locate: false }, } ], events: mapEvt } return (
{ this.state.showMap && {this.state.markers} {this.state.polylines} { this.state.infoWindow.visible && } }
); } } Main.propTypes = { position: React.PropTypes.any, // 位置,可以是经纬度数组或LngLat实例 title: React.PropTypes.string, // 提示文字 zoom: React.PropTypes.number, // 缩放比例 }; Main.defaultProps = { zoom: 14, }; export default Main;