赞
踩
使用到的点位聚合插件是 leaflet.markercluster-src
git 地址:https://github.com/Leaflet/Leaflet.markercluster#customising-the-clustered-markers
import "./css/screen.css"
import "./css/MarkerCluster.css"
import "./css/MarkerCluster.Default.css"
import "./js/leaflet.markercluster-src"
let polyMarkers={
station:[],
moun:[],
patrol:[],
physical:[],
car:[],
face:[],
video:[],
phone:[],
wifi:[],
high:[]
}
markerArr.forEach((item)=>{
polyMarkers[item]=L.markerClusterGroup();
})
let markerArr=["station","moun","patrol","physical","car","face","video","phone","wifi","high"] let data=[ { "id": 2, "lat": "36.386719", "lng": "119.047852", "title": "岗亭1", "type": "moun", "state": 1 }, { "id": 3, "lat": "28.300781", "lng": "115.532227", "title": "岗亭2", "type": "moun", "state": 1 }, { "id": 4, "lat": "36.964249", "lng": "118.073521", "title": "警务站1", "type": "station", "state": 1 }, { "id": "37032101001316004014", "lat": "36.949660778045654", "lng": "118.09536159038544", "title": "车载12_巡特警x鲁xC-2898警", "type": "patrol", "state": 1 }, { "id": "37032101001316004013", "lat": "36.960850954055786", "lng": "118.09093058109283", "title": "车载07_巡特警鲁xxxcC-2789警", "type": "patrol", "state": 1 }, { "id": 32, "lat": "", "lng": "", "title": "巡逻队员高", "type": "physical", "state": 0, "iconType": "workPeople", "cid": "3" }, { "id": 33, "lat": "", "lng": "", "title": "执勤民警", "type": "physical", "state": 0, "iconType": "workPeople", "cid": "2" }, { "id": 34, "lat": "", "lng": "", "title": "xxxxx", "type": "physical", "state": 1, "iconType": "workPeople", "cid": "2" } ] //地图图标默认全选 const [markerTo, setMarkerTo] = useState({ station: true, stationData: [], moun: true, patrol: true, physical: true, unPhysical: true, car: true, face: true, video: true, phone: true, wifi: true, status: true, high: true });
const polyMarker=data=>{ if(map){ data.forEach((elem,index)=>{ let iconHtml = ``,opts=``; iconHtml = iconTemplate(elem); opts=L.icon({ iconUrl: iconHtml, iconSize: [20, 20], iconAnchor: [12, 12], className: `${ elem.state ? `device device-${index} marker-status-${elem.type} ` : `device marker-status-${elem.type} unnormal ` }` }); let marker = L.marker([elem.lat, elem.lng], { icon: opts }); marker.data = elem; marker.bindTooltip(`<span>${elem.title}</span>`, { offset: [10, -10], direction: "right" }); markers.push(marker); marker.addEventListener('click',function(e){ markerIconShow(elem) }) markerArr.forEach((item)=>{ if(elem.type==item){ if(elem.state==2){ unPolyMarkers[item].addLayer(marker) }else{ polyMarkers[item].addLayer(marker) } } }) }) markerArr.forEach((item)=>{ map.addLayer(polyMarkers[item]) map.addLayer(unPolyMarkers[item]) }) } }
ps:效果图
markerValue就是你点击的当前按钮的type值,通过比对删除对应的图层,其中使用eval 来计算出需要显示的type+Data ,得到在上面对数据进行分组的数据。
//地图底部图标切换 地图点位的marker const markerToggle = (e, type) => { setMarkerValue(type); setMarkerTo({ ...markerTo, [type]:!markerTo[type], status:2 }) }; //加载,隐藏地图marker useEffect(() => { if (markerTo.status == 2) { polyMarkers[markerValue].clearLayers() } else { let data=eval(markerValue+"Data") polyMarker(data) } } }, [markerTo]);
效果图:
markerArr.forEach((item,index)=>{
polyMarkers[item]=L.markerClusterGroup({iconCreateFunction: function(cluster) {
return L.divIcon({ html: `<div class="polyMarker-box marker-status-${item}">
<span class="polyMarker-box-${item} polyMarker-${item} "></span>
<span> ${cluster.getChildCount()}</span></div>` });
}});
})
效果图
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。