|
|
@ -17,6 +17,7 @@ function BaseMap(props) { |
|
|
|
setPointData=()=>{},//传给父组件点位 |
|
|
|
data=[], |
|
|
|
centerPoint=[], |
|
|
|
childInfo={} |
|
|
|
|
|
|
|
} = props; |
|
|
|
|
|
|
@ -33,8 +34,16 @@ function BaseMap(props) { |
|
|
|
|
|
|
|
const [map, setMap] = useState(null); |
|
|
|
const mapRef = useRef(map); |
|
|
|
//信息,state与ref一起使用 |
|
|
|
const [info,setInfo]=useState({}) |
|
|
|
const infoRef=useRef(false) |
|
|
|
// /展示隐藏info, |
|
|
|
const [showInfo,setShowInfo]=useState(false) |
|
|
|
|
|
|
|
const infoDataRef=useRef({}) |
|
|
|
mapRef.current = map; |
|
|
|
const [hasInit, setHasInit] = useState(false); // 地图是否已经创建 |
|
|
|
|
|
|
|
let style = [{ |
|
|
|
url: Charge50, |
|
|
|
anchor: new AMap.Pixel(22, 22), |
|
|
@ -95,6 +104,7 @@ function BaseMap(props) { |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
initMap(); |
|
|
|
|
|
|
@ -102,6 +112,15 @@ function BaseMap(props) { |
|
|
|
// mapRef.current.remove(); |
|
|
|
}; |
|
|
|
}, []); |
|
|
|
useEffect(()=>{ |
|
|
|
if(JSON.stringify(childInfo)!="{}"){ |
|
|
|
setInfo(childInfo) |
|
|
|
infoDataRef.current=childInfo |
|
|
|
setShowInfo(true) |
|
|
|
infoRef.current=true |
|
|
|
} |
|
|
|
},[childInfo]) |
|
|
|
console.log(info); |
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
if (map&&data.length!=0) { |
|
|
@ -112,28 +131,45 @@ function BaseMap(props) { |
|
|
|
style: style |
|
|
|
}); |
|
|
|
var marker = new AMap.Marker({ content: ' ', map: map }); |
|
|
|
console.log(mass); |
|
|
|
mass.on('mouseover', function (e) { |
|
|
|
|
|
|
|
marker.setPosition(e.data.lnglat); |
|
|
|
marker.setLabel({ |
|
|
|
content: ` |
|
|
|
<div class="map-item"> |
|
|
|
<div class="name">${e.data.name}</div> |
|
|
|
<div class="location"><img src=${mapLocation} title=${e.data.location} />${e.data.location}</div> |
|
|
|
<div class="pic"><img src=${e.data.pic} /></div> |
|
|
|
<div class="item"><div>慢充占用</div><div>${e.data.slow_charge}</div></div> |
|
|
|
<div class="item"><div>快充占用</div><div>${e.data.fast_charge}</div></div> |
|
|
|
<div class="item"><div>所属厂家</div><div>${e.data.factory}</div></div> |
|
|
|
</div> |
|
|
|
`, |
|
|
|
}) |
|
|
|
|
|
|
|
mass.on('click', function (e) { |
|
|
|
if(!infoRef.current){ |
|
|
|
setInfo(e.data) |
|
|
|
infoDataRef.current=e.data |
|
|
|
setShowInfo(true) |
|
|
|
infoRef.current=true |
|
|
|
}else{ |
|
|
|
if(e.data.lnglat.lat==infoDataRef.current.lnglat.lat){ |
|
|
|
setShowInfo(false) |
|
|
|
infoRef.current=false |
|
|
|
}else{ |
|
|
|
setInfo(e.data) |
|
|
|
infoDataRef.current=e.data |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// marker.setPosition(e.data.lnglat); |
|
|
|
// marker.setLabel({ |
|
|
|
// content: ` |
|
|
|
// <div className="map-item"> |
|
|
|
// <div className="name">${e.data.name}</div> |
|
|
|
// <div className="location"><img src=${mapLocation} title=${e.data.location} />${e.data.location}</div> |
|
|
|
// <div className="pic"><img src=${e.data.pic} /></div> |
|
|
|
// <div className="item"><div>慢充占用</div><div>${e.data.slow_charge}</div></div> |
|
|
|
// <div className="item"><div>快充占用</div><div>${e.data.fast_charge}</div></div> |
|
|
|
// <div className="item"><div>所属厂家</div><div>${e.data.factory}</div></div> |
|
|
|
// </div> |
|
|
|
// `, |
|
|
|
// }) |
|
|
|
}); |
|
|
|
mass.on('mouseout', function (e) { |
|
|
|
// mass.on('mouseout', function (e) { |
|
|
|
|
|
|
|
// marker.setPosition(e.data.lnglat); |
|
|
|
// marker.setLabel() |
|
|
|
}); |
|
|
|
|
|
|
|
// }); |
|
|
|
mass.setMap(map); |
|
|
|
//这里经纬度需要注意,data里面的lnglat数据格式不知道为什么给改了 |
|
|
|
map.setZoomAndCenter(15, [data[0].lnglat.lng,data[0].lnglat.lat]) |
|
|
@ -156,6 +192,14 @@ function BaseMap(props) { |
|
|
|
<div> |
|
|
|
<div id="panel"></div> |
|
|
|
{hasInit ? renderChildren(map) : null} |
|
|
|
{ showInfo&&<div className="map-item"> |
|
|
|
<div className="name">{info.name}</div> |
|
|
|
<div className="location"><img src={mapLocation} title={info.location} />{info.location}</div> |
|
|
|
<div className="pic"><img src={info.pic} /></div> |
|
|
|
<div className="item"><div>慢充占用</div><div>{info.slow_charge}</div></div> |
|
|
|
<div className="item"><div>快充占用</div><div>{info.fast_charge}</div></div> |
|
|
|
<div className="item"><div>所属厂家</div><div>{info.factory}</div></div> |
|
|
|
</div>} |
|
|
|
</div> |
|
|
|
</div> |
|
|
|
); |
|
|
|