import React, { Component } from 'react';
import {observer} from 'mobx-react';
import { WeaLocaleProvider, WeaDialog, WeaMap } from 'ecCom';
import MapUtil from '../../util/mapUtil';
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.signTime}
${c.signTitle}
${c.information ? `
${c.information}
` : ''}
${c.pics ? `
${picDom}
` : ''}
${crm && crm.length > 0 ?
`
` : ''}
`;
}
@observer
export default class LocationInfo extends Component{
setMapRef = ref => {
if(ref && this.mapUtil == null){
this.mapUtil = new MapUtil(ref);
window.closeHrmMapInfoWindow = this.closeInfoWindow;
// this.props.store.draw = this.draw;
}
}
amapEvents = {
created: instance => {
this.mapUtil.mapInstance = instance;
this.draw();
}
}
initEndCallback = () => {
// this.props.store.draw = this.draw;
this.draw();
}
closeInfoWindow = () => {
this.mapUtil && this.mapUtil.closeInfoWindow();
}
draw = () => {
if(!this.mapUtil)
return;
const {store: {selectedSignInfo: d}} = this.props;
const {isAmap, isGoogleMap, clearMapData, addMarker, addPolyline, setFitView} = this.mapUtil;
//清理地图数据
clearMapData();
//自适应数据
const boundDatas = [];
const date = moment(d.date, 'YYYY-MM-DD').format('YYYY-MM-DD');
const {x: longitude, y: latitude} = d || {};
if(longitude && latitude){
d.signTime = `${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);
isGoogleMap && boundDatas.push({latitude, longitude});
// idx++;
}
// 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 || {};
// })
// })
// //添加polyline
// polylinePath.length > 0 && addPolyline(polylinePath);
//地图元素自适应
setFitView(boundDatas);
}
render(){
const {store} = this.props;
const {locationInfoDialogProps, selectedSignInfo} = store;
const mapProps = { zoom: 14, onMapSwitch: type => {}, events: this.amapEvents, initGoogleEndCallback: this.initEndCallback};
return (
{
locationInfoDialogProps.visible && selectedSignInfo &&
this.setMapRef(ref)}/>
}
)
}
}