import React, { useState } from 'react';
import { Select, Input, Card, Col, Row, Button, message, Modal, Spin, Form, Alert, Space } from 'antd';
import { DoubleLeftOutlined, ExclamationCircleFilled } from '@ant-design/icons';
import LayerData from './LayerData';
// import LayerList from './LayerList';
import { AimOutlined, DoubleRightOutlined } from '@ant-design/icons';
import Header from '../../components/Header/index.js';
import { createPoint, updatePoint, getDetailByIdApi, queryUserLayers, getLayerPointsApi, getAllProvinceApi, getCityByProvinceApi, getDistrictByCityApi, getLayerShapesApi, getPointByAddressApi, deletePoint } from '../../api';
import Pop from './pop'
import { GaodeMap, Scene, Heatmap, PolygonLayer, Marker, MarkerLayer, Popup, MouseLocation, CanvasLayer, Scale, MapTheme, Zoom } from '@antv/l7';
const { confirm } = Modal;
// import { DrawPoint, DrawEvent } from '@antv/l7-draw';
import icon04 from '../../assets/icon/icon04.svg'
import styles from './index.less';
import './index.less';
import pop from './pop';
import { remove } from 'react-cookies';
const { Option } = Select;
const nullSearchLocation = () => {
message.error('坐标信息为空');
};
const nullSearchAddress = () => {
message.error('地址信息为空');
};
const errorSearchAddress = () => {
message.error('地址搜索信息异常,请联系开发人员');
};
const successSearchAddress = () => {
message.success('已成功进行地图定位');
};
// 省编码(全局)
let ProvinceAdcode = null;
let ProvinceData = null;
// 市编码(全局)
let CityAdcode = null;
let CityData = null;
// 区编码(全局)
let DistrictAdcode = null;
let DistrictData = null;
class BaseMap extends React.Component {
constructor() {
super();
this.state = {
map: null,
// 地图中心
center: {
// title: '石家庄',longitude: 114.502461,latitude: 38.045474
},
provinceList: [],// 省
provinceAdcode: '',//选中的省
provinceLabel: '',//选中的省
cityList: [],//市
cityAdcode: '',//选中的市
cityLabel: '',
districtList: [],//区
districtAdcode: '',//选中的区
zoom: 4,
inputExplainKey: 0,
inputExplainValue: "请输入地址详情信息",
inputContent: '',
heatMapList: [],
loading: false,
markerLayerList: [], // 点位图层
isModalOpen: false,
isEditModalOpen: false,
editPointId: '',
selectLayerList: [],
pointAdd: {},
pointEdit: {},
pointDrawer: null,
newPointLocation: '',
mouseLocation: {},
disabled: false,
popParameter: {},
isAlertOpen: false,
deletePointId: '',
pop: null,
removeMarker: null,
autoComplete: null, // 地图搜索提示
}
}
startLoading = () => {
this.setState({
loading: true,
});
}
stopLoading = () => {
this.setState({
loading: false,
});
}
// 钩子,打开执行
componentDidMount = () => {
this.getAllProvince();
this.createMap();
}
componentWillUnmount = () => {
// 组件卸载时,清除地图
this.state.map.map.destroy();
this.state.map.destroy();
this.setState = null;
console.log('组件卸载时,清除地图');
}
// 创建地图
createMap = () => {
const scene = new Scene({
id: 'container',
map: new GaodeMap({
pitch: 20,
style: 'light',
center: [118.405285, 39.904989],
zoom: 3,
plugin: ['AMap.AutoComplete', 'AMap.PlaceSearch'],
})
});
this.setState({
map: scene,
}, () => {
// 地图拖拽鼠标图标更改
let can = document.getElementsByClassName('amap-layer')[0];
can.addEventListener('mousedown', (e) => {
console.log('mousedown', can);
// can.className = 'amap-layer cursor-grab';
can.className = can.className.replace("amap-layer", "amap-layer cursor-grab");
can.addEventListener('mouseup', (e) => {
can.className = can.className.replace("cursor-grab", " ");
// console.log('mouseup',can.className.replace("cursor-grab"," "));
});
})
});
// 地图搜索提示功能
scene.on('loaded', () => {
// 比例尺插件
const scale = new Scale();
scene.addControl(scale);
// 地图主题插件
const mapTheme = new MapTheme({
position: 'bottomright', // 控件的位置
options: [
{
"text": "",
"value": "amap://styles/normal",
"img": "https://gw.alipayobjects.com/mdn/rms_816329/afts/img/A*-nqiT6Vu948AAAAAAAAAAAAAARQnAQ",
"key": "normal"
},
{
"text": "",
"value": "amap://styles/c422f5c0cfced5be9fe3a83f05f28a68?isPublic=true",
"img": "https://gw.alipayobjects.com/mdn/rms_816329/afts/img/A*J_wYQL_PaUEAAAAAAAAAAAAAARQnAQ",
"key": "light"
},
{
"text": "",
"value": "amap://styles/c9f1d10cae34f8ab05e425462c5a58d7?isPublic=true",
"img": "https://gw.alipayobjects.com/mdn/rms_816329/afts/img/A*U7M9QI1yat4AAAAAAAAAAAAAARQnAQ",
"key": "dark"
},
{
"text": "",
"value": "amap://styles/fresh",
"img": "https://gw.alipayobjects.com/mdn/rms_816329/afts/img/A*T-oBT4hB5ucAAAAAAAAAAAAAARQnAQ",
"key": "fresh"
},
{
"text": "",
"value": "amap://styles/grey",
"img": "https://gw.alipayobjects.com/mdn/rms_816329/afts/img/A*OREXQ4vgQRIAAAAAAAAAAAAAARQnAQ",
"key": "grey"
},
{
"text": "",
"value": "amap://styles/graffiti",
"img": "https://gw.alipayobjects.com/mdn/rms_816329/afts/img/A*4UApTKmeiy4AAAAAAAAAAAAAARQnAQ",
"key": "graffiti"
},
{
"text": "",
"value": "amap://styles/macaron",
"img": "https://gw.alipayobjects.com/mdn/rms_816329/afts/img/A*0GrCQLtDjNcAAAAAAAAAAAAAARQnAQ",
"key": "macaron"
},
{
"text": "",
"value": "amap://styles/darkblue",
"img": "https://gw.alipayobjects.com/mdn/rms_816329/afts/img/A*uWxqSZQlPkkAAAAAAAAAAAAAARQnAQ",
"key": "darkblue"
},
{
"text": "",
"value": "amap://styles/wine",
"img": "https://gw.alipayobjects.com/mdn/rms_816329/afts/img/A*OFPrTbg3an0AAAAAAAAAAAAAARQnAQ",
"key": "wine"
}
]
});
scene.addControl(mapTheme);
let op = mapTheme.getOptions();
console.log('op', op);
// AutoComplete(输入框提示) 插件
window.AMap.plugin(['AMap.AutoComplete', 'AMap.PlaceSearch'], () => {
// Autocomplete 实例配置
const autoOptions = {
city: '全国', //城市,默认全国
input: "tipinput", //使用联想输入的input的id
citylimit: true,
// closeResultOnScroll: false
};
// 创建Autocomplete对象
const autoComplete = new AMap.AutoComplete(autoOptions);
// 监听输入框的focus事件,否则总是不触发提示
document.getElementById('tipinput').addEventListener('focus', (e) => {
autoComplete.search(e.target.value, (status, result) => {
// 搜索成功时,result即是对应的匹配数据
// console.log('result', result);
});
});
this.state.autoComplete = autoComplete;
// 监听选中事件,将选中的地址信息显示在输入框中,暂时不需要,注解掉了
autoComplete.on('select', (e) => {
let adcode = e.poi.adcode;
console.log('select', e);
this.setState({
inputContent: e.poi.name, // 将选中的地址信息显示在输入框中
// provinceAdcode: adcode.substr(0, 2) + '0000',
}, () => {
// 改变adcode后不自动触发onProvinceChange函数,手动触发
// let data = this.state.provinceList.find(o => o.value === this.state.provinceAdcode);
// this.onProvinceChange(this.state.provinceAdcode, data);
})
})
// 根据省市区码加载地图
if (ProvinceAdcode) {
this.onProvinceChange(ProvinceAdcode, ProvinceData);
}
if (CityAdcode) {
this.onCityChange(CityAdcode, CityData);
}
if (DistrictAdcode) {
this.onDistrictChange(DistrictAdcode, DistrictData);
}
});
// scene.map.plugin(['AMap.Autocomplete'], () => {
// // Autocomplete 实例配置
// const autoOptions = {
// city: '全国', //城市,默认全国
// input: 'tipinput' //使用联想输入的input的id
// };
// // 创建Autocomplete对象
// const autoComplete = new AMap.autoComplete(autoOptions);
// });
})
}
// 关闭点位窗口的提示界面
// Elmessage
//设置地图省份级别位置
gotoProvince = (data) => {
let zoom = data.zoom || 6;
let lng = data.len || 116.405285;
let lat = data.lat || 39.904989;
this.state.map.setZoomAndCenter(zoom, [lng, lat]);
}
//根据cityname、adcode、citycode设置地图位置
gotoCity = (data) => {
this.state.map.removeAllMarkers && this.state.map.removeAllMarkers();
let zoom = data.zoom || 10;
let lng = data.len || 116.405285;
let lat = data.lat || 39.904989;
this.state.map.setZoomAndCenter(zoom, [lng, lat]);
}
// 获取图层明细点位数据/热力图数据
getLayerPoints = (layerId, selected, nodeType) => {
if (selected) {
this.startLoading(); // 开启加载动画
getLayerPointsApi({
adcode: this.state.districtAdcode || this.state.cityAdcode || this.state.provinceAdcode,
layerId: layerId
}).then((e) => {
console.log("网络请求完成")
// 获取点位数据
let layerPointsData = e.data || [];
layerPointsData = layerPointsData.map((item) => {
item.lng = item.location.split(',')[0];
item.lat = item.location.split(',')[1];
item.value = 15;
return item;
});
if (nodeType === 1) {
// 在地图上添加点位
this.setMarkers(layerPointsData, layerId);
} else if (nodeType === 4) {
// this.setHeatMap(layerPointsData, layerId);
}
}).then(() => {
this.stopLoading();
});
} else {
let self = this;
let getLayers = self.state.markerLayerList;
for (let i = 0; i < getLayers.length; i++) {
if (getLayers[i].markerLayerOption.name == layerId) {
self.state.map.removeMarkerLayer(getLayers[i]);
}
}
self.hideHeatMap(layerId);
}
}
// 隐藏热力图
hideHeatMap = (layerId) => {
let heatMapList = this.state.heatMapList
for (let i = 0; i < heatMapList.length; i++) {
if (heatMapList[i].og.data[0].id === layerId || layerId === undefined || layerId === null || layerId === '') {
heatMapList[i].hide();
}
}
}
// 在地图上设置点位 【经度,纬度】
setMarkers = (data, layerId) => {
let self = this;
let map = this.state.map;
let LabelsData = data || [];
let markerLayer = new MarkerLayer({ name: layerId });
let el, popup, marker, icon, popHtml;
// 设置地图上的点位(图标)
for (let i = 0; i < LabelsData.length; i++) {
el = document.createElement('label');
el.style.width = '26px';
el.style.height = '26px';
el.id = 'marker' + i;
icon = LabelsData[0].logoImage;
if (icon && (icon.substr(0, 7) == "http://" || icon.substr(0, 6) == "https:" || icon.substr(0, 7) == "http:\\\\")) {
el.style.background = 'url("' + icon + '") no-repeat';
el.style.backgroundSize = 'cover';
} else {
el.className = icon;
}
marker = new Marker({
element: el,
}).setLnglat({ lng: LabelsData[i].lng, lat: LabelsData[i].lat })
// 设置弹框(点击事件)
marker.on('click', (e) => {
// 获取弹框内的html
this.setState({ popParameter: LabelsData[i] }, () => {
popHtml = document.getElementById('custom-pop');
popup = new Popup({
offsets: [0, 30],
closeOnClick: true,
autoClose: true,
})
.setLnglat(e.lngLat)
.setHTML(popHtml.innerHTML)
document.getElementById('marker' + i).style.width = '32px';
document.getElementById('marker' + i).style.height = '32px';
// 给弹框添加事件
popup.on('hide', () => {
document.getElementById('marker' + i).style.width = '26px';
document.getElementById('marker' + i).style.height = '26px';
});
this.setState({ pop: popup })
popup.on('open', () => {
// 编辑按钮
document.getElementsByClassName('edit-button')[0].onclick = () => {
// getDetailByIdApi({id:LabelsData[i].pointId,layerId:LabelsData[i].belongLayerId}).then((e) => {
// if (e.success) {
// console.log('获取点位详情成功',e);
// }else{
// alert('获取点位详情失败');
// console.log('获取点位详情失败',e);
// }
// })
if (this.state.cityAdcode == '' && this.state.districtAdcode == '') {
message.error('请至少选择行政区域至城市再进行点位创建');
return false;
}
let selectLayerList = this.refs.getLayerDataFun.state.selectLayerList;
selectLayerList = selectLayerList.filter((item) => {
if (item.isCustomize == 1) {
return item;
}
})
if (selectLayerList.length == 0) {
message.error('请至少选中一个个人图层');
return false;
};
this.setState({
selectLayerList: selectLayerList,
pointEdit:
{
pointId: LabelsData[i].pointId,
layerId: LabelsData[i].belongLayerId,
name: LabelsData[i].pointName,
// note: LabelsData[i].note,
address: LabelsData[i].address,
},
isEditModalOpen: true
})
};
// 删除按钮
document.getElementsByClassName('delete-button')[0].onclick = () => {
confirm({
icon: