import React, { useEffect, useState } from 'react';
import { Tooltip } from 'antd';
import { connect } from 'react-redux';
import { render } from 'react-dom';
import { useMockRequest, useFsRequest } from '$utils';
import { communtity_data } from '../constants/index'
import moment from 'moment';
import './gis.less'
const MAPDOMID = 'fs-amap-container';
let map = null;
const TYPES = {
1: '消火栓',
2: '水箱',
3: '烟感',
4: '配电箱'
}
function Map(props) {
const [delay, setDelay] = useState(true)
const [tab, setTab] = useState('home')
const [type, setType] = useState(3)
const { data: devices = [] } = useMockRequest({
url: 'https://superchangnan.anxinyun.cn/api/xiaofang/devices',
method: 'mockGet',
});
const { data: communtityInfo = {} } = useFsRequest({ url: 'community/info' });
// 地图初始化
const loadMap = () => {
// 图片图层 实现瓦片地图中国地图样式 bounds 第一个点为左下角 第二个点为右上角
const imageLayer = new AMap.ImageLayer({
url: '/assets/images/map.svg',
bounds: new AMap.Bounds(
[115.800221, 28.265659],
[116.334849, 28.973298],
),
zooms: [3, 14],
});
map = new AMap.Map(MAPDOMID, {
center: [116.054664, 28.538966],
zoomEnable: true,
dragEnable: true,
viewMode: '3D',
pitch: 22.9,
labelzIndex: 130,
zoom: 10.3,
cursor: 'pointer',
mapStyle: 'amap://styles/4eb48d1ef0a024c73376fd2256d0b5a5',
layers: [
AMap.createDefaultLayer(),
imageLayer,
],
});
map.on('complete', () => {
setDelay(false)
// tab == 'home' && map && renderMarkers()
});
map.on('click', (e) => {
if (!e && !e.lnglat) {
return
}
const zoom = map.getZoom();
const pitch = map.getPitch();
const center = map.getCenter();
const Rotation = map.getRotation();
console.log('zoom' + zoom)
console.log('pitch' + pitch)
console.log('center' + center)
console.log('Rotation' + Rotation)
console.log('e.lnglat' + e.lnglat)
})
};
useEffect(() => {
loadMap();
}, []);
useEffect(() => {
if (!delay) {
setTimeout(() => {
tab == 'home' && map && renderMarkers()
tab == 'device' && map && renderDevices()
}, 200);
}
}, [tab, devices]);
useEffect(() => {
if (!delay) map && renderDevices()
}, [type]);
const renderMarkers = () => {
map.clearMap();
map.setZoom(10.3)
map.setCenter([116.054664, 28.538966])
map.setPitch(22.9)
map.setRotation(1.7000)
const data = communtity_data
const markers = []
//下钻点位显示
data.map((x, index) => {
var marker = new AMap.Marker({
position: new AMap.LngLat(x.lng, x.lat),
// 将一张图片的地址设置为 icon
icon: `/assets/images/homepage/communtity/${x.name}.png`,
// 设置了 icon 以后,设置 icon 的偏移量,以 icon 的 [center bottom] 为原点
offset: new AMap.Pixel(-69, -70),
zooms: [3, 19],
});
marker.setTitle(x.name);
map.add(marker);
markers.push(marker)
let infowindow = new AMap.InfoWindow({
isCustom: true, //使用自定义窗体
content: `
`,
offset: new AMap.Pixel(233, 260)
});
marker.on('click', () => {
let position = marker.getPosition ? marker.getPosition() : marker.getCenter();
infowindow.open(map, position);
map.setCenter(position)
setTimeout(() => {
let inuse = communtityInfo?.communityInUse?.find(v => v.houseCommunity == x.name)?.count || 0
let unuse = communtityInfo?.communityUnUse?.find(v => v.houseCommunity == x.name)?.count || 0
if (document.getElementById(`contentid${x.name}`)) {
render(
{
map.setCenter([115.922069, 28.554867])
map.clearInfoWindow();
}} />
房龄:
{moment().diff(moment(x.build), 'years')}年
租赁中:
{inuse}套
空置:
{unuse}套
,
document.getElementById(`contentid${x.name}`));
}
}, 50)
})
})
// map.setFitView(markers, false, [150, 150, 400, 400])
// data.map((x, index) => {
// var marker = new AMap.Marker({
// position: new AMap.LngLat(x.lng, x.lat),
// // 将一张图片的地址设置为 icon
// // 设置了 icon 以后,设置 icon 的偏移量,以 icon 的 [center bottom] 为原点
// // offset: new AMap.Pixel(-13, -30),
// zooms: [3, 19],
// });
// marker.setTitle(x.name);
// map.add(marker);
// })
}
const renderDevices = () => {
map.clearMap()
var style = [{
url: '/assets/images/homepage/communtity/gishydrant.png',
anchor: new AMap.Pixel(6, 6),
size: new AMap.Size(11, 11)
}, {
url: '/assets/images/homepage/communtity/giswatertank.png',
anchor: new AMap.Pixel(4, 4),
size: new AMap.Size(11, 11)
}, {
url: '/assets/images/homepage/communtity/gissmoke.png',
anchor: new AMap.Pixel(3, 3),
size: new AMap.Size(11, 11)
}, {
url: '/assets/images/homepage/communtity/giselectricity.png',
anchor: new AMap.Pixel(3, 3),
size: new AMap.Size(11, 11)
}];
let points = [], markers = [];
devices?.filter(x => x.type == type)?.map(x => {
let data = JSON.parse(x.data)
data?.map((s, index) => {
points.push(
{
lnglat: new AMap.LngLat(s.lng, s.lat),
style: x.type - 1,//对应的status相对应的样式style
name: s.address,
id: s.address + s.type + index,
data: s,
}
);
// if (index < 2000 && s.lng && s.lat) {
// const marker = new AMap.Marker({
// icon: '/assets/images/homepage/fire/leader.png',
// // icon: "//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png",
// position: [s.lng, s.lat],
// // offset: new AMap.Pixel(-13, -30),
// // content: ''
// // size: [13, 13]
// });
// marker.setTitle(index);
// marker.setMap(map);
// }
});
})
var mass = new AMap.MassMarks(points, {
opacity: 0.8,
zIndex: 11111111,
cursor: 'pointer',
style: style
});
mass.on('click', function (e) {
let id = Math.random(1000)
map.setCenter(e.data.lnglat)
let infowindow = new AMap.InfoWindow({
isCustom: true, //使用自定义窗体
content: `
`,
offset: new AMap.Pixel(181, 260)
});
infowindow.open(map, e.data.lnglat)
setTimeout(() => {
if (document.getElementById('info-window' + id)) {
render(
{ map.clearInfoWindow() }} />
地址
{e.data.name?.length > 20 ? e.data.name?.substring(0, 20) + '...' : e.data.name}
,
document.getElementById('info-window' + id));
}
}, 50)
window.closeTooltip = () => {
testDiv.close();
}
})
mass.setMap(map)
}
const renderLeftTop = () => {
return
廉租房总数
{communtityInfo ? (communtityInfo?.inuse + communtityInfo?.unuse) : '-'}
租赁中
{communtityInfo ? (communtityInfo?.inuse) : '-'}
空置
{communtityInfo ? (communtityInfo?.unuse) : '-'}
}
const renderRightBottom = () => {
return
{[
{ name: '烟感设备', icon: 'gissmoke', type: 3 },
{ name: '配电箱', icon: 'giselectricity', type: 4 },
{ name: '消火栓', icon: 'gishydrant', type: 1 },
{ name: '水箱', icon: 'giswatertank', type: 2 }
].map(s =>
{ setType(s.type) }} className={`checkbox`} >{s.type == type ? '✓' : ''}
{s.name}
)}
}
return (
<>
{/* 延缓加载遮罩 */}
{delay &&
}
{/* 地图容器 */}
{/* 底部按钮 */}
{[
{ name: '房屋分布', tab: 'home' },
{ name: '人口分布', tab: 'person' },
{ name: '基础设施', tab: 'device' }
].map((s, index) => {
return index == 1 ? '' :
{
if (delay) return
setTab(s.tab)
}}
style={{ zIndex: 1001 }}
>
{s.name}
})
}
{/* 左上角图例 */}
{tab == 'home' && renderLeftTop()}
{tab == 'device' && renderRightBottom()}
{/* 四周遮罩 */}
>
);
}
export default connect()(Map);