import React, { Component } from 'react';
import {renderToString} from 'react-dom/server'
import { WeaAlertPage, WeaLocaleProvider, WeaMap } from 'ecCom';
import { observer } from 'mobx-react';
import { toJS } from 'mobx';
import MapUtil from '../../util/mapUtil';
import isEmpty from 'lodash/isEmpty';
import {addContentPath} from '../../util/index.js';
import moment from 'moment';
const getLabel = WeaLocaleProvider.getLabel;
const createDetailTemplete = (c, isGoogleMap = false) => {
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 ?
`
` : ''}
`;
}
@observer
export default class MapView extends Component {
componentDidMount() {
window.closeHrmMapInfoWindow = this.closeInfoWindow;
const { newOutSide } = this.props;
const { getData, init } = newOutSide;
newOutSide.redraw = this.draw;
getData();
}
setMapRef = ref => {
if(ref && this.mapUtil == null){
this.mapUtil = new MapUtil(ref);
}
}
amapEvents = {
created: instance => {
this.mapUtil.mapInstance = instance;
this.draw();
}
}
initEndCallback = () => {
this.draw();
// const {isAmap, isGoogleMap} = this.mapUtil;
// console.debug(isAmap, isGoogleMap)
}
closeInfoWindow = () => {
this.mapUtil && this.mapUtil.closeInfoWindow();
}
draw = () => {
if(!this.mapUtil)
return;
const {newOutSide} = this.props;
const mapDatas = toJS(newOutSide.mapData);
const {isAmap, isGoogleMap, clearMapData, addMarker, addPolyline, setFitView} = this.mapUtil;
//清理地图数据
clearMapData();
//自适应数据
const boundDatas = [];
const polylinePath = [];
let idx = 1;
//添加marker
mapDatas.map((d, i) => {
const date = moment(d.date, 'YYYY-MM-DD').format('YYYY-MM-DD');
d.items.reverse().map((d, i) => {
const {x: longitude, y: latitude} = d || {};
if(longitude && latitude){
d.time = `${date} ${d.time}`;
const otherParams = {};
isAmap && Object.assign(otherParams, {
offset: new window.AMap.Pixel(-17, -34),
title: d.title,
content: `${idx}
`,
events: [
{
event: 'click',
onEventHandle: e => {
const data = {marker: e.target, content: createDetailTemplete(d), isCustom: true};
this.mapUtil.openInfoWindow(data);
}
}
]
});
isGoogleMap && Object.assign(otherParams, {
events: [
{
event: 'click',
onEventHandle: () => {
const data = {content: createDetailTemplete(d, true), isCustom: true};
this.mapUtil.openInfoWindow(data, {
position: {lat: Number(latitude), lng: Number(longitude)}
});
}
}
]
})
isGoogleMap && Object.assign(otherParams, {label:{text:String(i + 1), color:'#fff'}});
const marker = addMarker({latitude, longitude}, null, null, otherParams);
isAmap && boundDatas.push(marker) && polylinePath.push([longitude, latitude]);
isGoogleMap && boundDatas.push({latitude, longitude}) && polylinePath.push({lat: Number(latitude), lng: Number(longitude)});
idx++;
}
})
})
//添加polyline
polylinePath.length > 0 && addPolyline(polylinePath);
//地图元素自适应
setFitView(boundDatas);
}
render(){
const {newOutSide} = this.props;
const mapDatas = toJS(newOutSide.mapData);
if(isEmpty(mapDatas)){
return (
}>
{getLabel('386505', '当天无外勤签到记录')}
);
}else{
const mapProps = { zoom: 14, onMapSwitch: type => {}, events: this.amapEvents, initGoogleEndCallback: this.initEndCallback};
return (
)
}
}
}