pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

<legend id='vwFr8'><style id='vwFr8'><dir id='vwFr8'><q id='vwFr8'></q></dir></style></legend>

<tfoot id='vwFr8'></tfoot>

    <i id='vwFr8'><tr id='vwFr8'><dt id='vwFr8'><q id='vwFr8'><span id='vwFr8'><b id='vwFr8'><form id='vwFr8'><ins id='vwFr8'></ins><ul id='vwFr8'></ul><sub id='vwFr8'></sub></form><legend id='vwFr8'></legend><bdo id='vwFr8'><pre id='vwFr8'><center id='vwFr8'></center></pre></bdo></b><th id='vwFr8'></th></span></q></dt></tr></i><div class="6yqkwg2" id='vwFr8'><tfoot id='vwFr8'></tfoot><dl id='vwFr8'><fieldset id='vwFr8'></fieldset></dl></div>
  1. <small id='vwFr8'></small><noframes id='vwFr8'>

      <bdo id='vwFr8'></bdo><ul id='vwFr8'></ul>

      react-leaflet:在渲染新標記之前清除標記簇

      react-leaflet: Clear marker cluster before rendering new markers(react-leaflet:在渲染新標記之前清除標記簇)

          <tbody id='b9GEV'></tbody>
        1. <legend id='b9GEV'><style id='b9GEV'><dir id='b9GEV'><q id='b9GEV'></q></dir></style></legend>
        2. <i id='b9GEV'><tr id='b9GEV'><dt id='b9GEV'><q id='b9GEV'><span id='b9GEV'><b id='b9GEV'><form id='b9GEV'><ins id='b9GEV'></ins><ul id='b9GEV'></ul><sub id='b9GEV'></sub></form><legend id='b9GEV'></legend><bdo id='b9GEV'><pre id='b9GEV'><center id='b9GEV'></center></pre></bdo></b><th id='b9GEV'></th></span></q></dt></tr></i><div class="sg2umaa" id='b9GEV'><tfoot id='b9GEV'></tfoot><dl id='b9GEV'><fieldset id='b9GEV'></fieldset></dl></div>

            <bdo id='b9GEV'></bdo><ul id='b9GEV'></ul>

              <tfoot id='b9GEV'></tfoot>

                <small id='b9GEV'></small><noframes id='b9GEV'>

                本文介紹了react-leaflet:在渲染新標記之前清除標記簇的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                問題描述

                TLDR;有沒有辦法使用 react-leaflet 和 react-leaflet-markercluster 從標記簇中清除所有標記?

                是的,有!我剛剛向 MarkerClusterGroup 添加了一個關(guān)鍵道具,正如@SethLutske 推薦的那樣.現(xiàn)在,每次重新渲染地圖時,之前的標記都會被移除,只保留新的標記.

                Yes, there is! I just added a key prop to the MarkerClusterGroup, as @SethLutske recommended. Now, every time there is a re-render of the map, the previous markers are removed and only the new ones remain.

                <MarkerClusterGroup
                    key={uuidv4()}  
                    spiderfyDistanceMultiplier={1}
                    showCoverageOnHover={true}
                >
                


                我在我所在州的公安機關(guān)工作.有一個 API 可以從我所在州的所需城市獲取監(jiān)控攝像頭的集合.我正在使用 React PWA 來檢索這些攝像頭的圖像,因此警察可以使用我的應(yīng)用程序進行實時監(jiān)控.警察在系統(tǒng)中進行身份驗證,使用表格按城市過濾攝像機,結(jié)果顯示在地圖中,地圖上帶有指示每個攝像機位置的標記.觸摸標記時,會顯示一個彈出窗口,其中包含該攝像機的信息,以及一個按鈕,觸摸時會在新頁面中顯示攝像機圖像.


                I work in the Public Security agency of my state. There is an API that gets a collection of monitoring cameras from a desired city of my State. I'm working in a React PWA that retrieves the image of these cameras, so the police can use my app for live monitoring. The policemen authenticate into the system, filter the cameras by their city using a form, and the result is shown in a map with markers that indicate the position of each camera. When touching a marker, a popup is shown with information of that camera and a button that shows the camera image in a new page when touched.

                地圖標記和彈出窗口

                我選擇 Leaflet 和 react-leaflet 進行地圖渲染,并選擇 react-leaflet-markercluster 來對彼此相鄰的標記進行分組.我有一個小問題:如果用戶按城市過濾相機,然后他返回表單并決定按不同城市過濾,我想要發(fā)生的行為是:

                I chose Leaflet and react-leaflet for the map rendering, and react-leaflet-markercluster to group the markers that are next each other. I'm having a little problem: if an user filters cameras by a city, then he returns to the form and decides to filter by a different city, the behavior that I wanted to happen is:

                • 清除上一個被過濾城市的所有標記;
                • 顯示當前城市的所有標記;

                但發(fā)生的情況是:前一個城市的標記仍保留在地圖中,彈出窗口為空.

                But what happens is this: the markers from the previous city remain in the map, with an empty popup.

                不同城市的標記組

                我的目標:我想在添加新城市的標記之前清除過去的城市標記!

                這是我的 MapContainer.盡管存在我提到的錯誤,但它運行良好.

                Here is how it is my MapContainer. It is working quite well, despite of the bug I mentioned.

                      <MapContainer            
                          center={[-26.627817, -51.196288]}
                          zoom={6}
                          scrollWheelZoom={false}
                          style={{ height: '100vh', width: '100wh' }}
                      >
                        {//<ClearLayers /> this clears the entire map, even the tile layer, leaving it blank
                        }
                          <TileLayer
                          attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
                          url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
                          />
                          <MarkerClusterGroup
                              spiderfyDistanceMultiplier={1}
                              showCoverageOnHover={false}
                          >
                              {cameras.map((camera: Camera) => (
                                  <Marker icon={iconBTV} key={camera.name} position={[camera.latitude, camera.longitude]}>
                                      <Popup>
                                        {camera.name} <br /> {camera.description} <br />
                                        <Link
                                          to={{
                                            pathname: "/cameraScreen",
                                          }}
                                          style={{ textDecoration: 'none' }}
                                        >
                                          <button
                                            style={{marginTop: "1rem"}}
                                            onClick={(e) => insertLog(e, camera)}
                                          >Ver camera</button>
                                        </Link>                            
                                      </Popup>
                                  </Marker>
                              ))}
                          </MarkerClusterGroup>
                      </MapContainer>
                

                我嘗試使用此功能,但它沒有按我的意愿工作.它會破壞地圖,使其完全空白.

                I tried using this function, but it doesn't work as I wanted. It bugs the map, leaving it completely blank.

                function ClearLayers() {
                  const map = useMap();
                  map.eachLayer(function (layer) {
                    map.removeLayer(layer);
                  });
                  return null
                }
                

                我在這里搜索并接近解決方案:我能夠通過使用 Leaflet 的核心 API 并留下 react-leaflet-markercluster 包裝器來清除過去的城市標記 如本答案所示.在向 MCG 添加新標記之前,我確實執(zhí)行了 clearLayers(),這樣就可以了.這是我嘗試做的:

                I've searched in here and came close to a resolution: I was able to clear the past city markers by using Leaflet's core API and leaving behind react-leaflet-markercluster wrapper as shown in this answer. I do execute clearLayers() before adding new markers to the MCG, and that does the trick. Here's what I tried to do:

                    import * as L from 'leaflet';
                    import 'leaflet.markercluster';
                
                    import { useMap } from 'react-leaflet';
                    import { useContext, useEffect } from 'react';
                    import { Link } from 'react-router-dom';
                    import CameraContext from '../../contexts/CameraContext';
                
                    const mcg = L.markerClusterGroup({
                        chunkedLoading: true,
                        showCoverageOnHover: false,
                    });
                
                    const MarkerCluster = ({ markers, icon }) => {
                        const { setCamera } = useContext(CameraContext);
                
                        const inserirLog = (e, camera) => {
                            setCamera(camera);
                            var ajax_log = new XMLHttpRequest();
                            ajax_log.open('GET', `https://fakeurl.sc.gov.br/api/ajax-log-bemtevi.php?camera=${camera.nome}&servidor=${camera.servidor}`);
                            ajax_log.send();
                        }   
                
                        console.log(markers);
                        const map = useMap();
                        useEffect(() => {
                            mcg.clearLayers();
                            const markerList = markers.map((camera) => {
                                return L.marker(new L.LatLng(camera.latitude, camera.longitude), {
                                    icon: icon,
                                }).bindPopup(
                                    `
                                    ${camera.name} <br /> ${camera.description} <br />
                                    <Link
                                    to={{
                                        pathname: "/cameraScreen",
                                    }}
                                    style={{ textDecoration: 'none' }}
                                    >
                                    <button
                                        style={{marginTop: "1rem"}}
                                        onClick={(e) => insertLog(e, camera)}
                                    >Watch camera</button>
                                    </Link>
                                    `
                                );
                            });
                            mcg.addLayers(markerList);
                            map.fitBounds(mcg.getBounds());
                            map.addLayer(mcg);
                        }, [markers, icon, map]);
                
                        return null;
                    };
                
                    export default MarkerCluster;
                

                通過這樣做,我無法在 Popup 中注入 JSX,也無法使用 react-router-dom 中的 Link 組件.我也無法將相機對象保存在上下文中以在下一頁中進一步使用它.所以我結(jié)束了堅持我當前的代碼,這是有效的,盡管有這個無法清除過去的城市標記".錯誤.

                By doing that, I can't inject JSX inside the Popup, and can't use the Link component from react-router-dom. I also couldn't save the camera object in a context to use it further in the next page. So I ended sticking to my current code, that is working, despite of having this "can't clear past city markers" bug.

                所以我想在使用 React 組件用于 Leaflet 地圖和 Marker Cluster 時有一種方法可以清除過去的城市標記.如果 MarkerClusterGroup 有一個鉤子,就像 Leaflet 地圖有一個鉤子一樣,我想這會更容易做到:我需要做的就是這樣簡單的事情:

                So I'd like to have a way to clear the past city markers when using the React components for Leaflet map and Marker Cluster. If there was a hook for the MarkerClusterGroup like there is one for the Leaflet map, I guess this would be easier to do: all I would need to do is something simple as this:

                    const markercluster = useMarkerClusterGroup();
                    markercluster.clearLayers();
                

                但是,據(jù)我所知,react-leaflet-markercluster 中沒有鉤子.

                But, as long as I know, there aren't hooks in react-leaflet-markercluster.

                推薦答案

                你可以在 MarkerClusterGroup 中添加一個 key 屬性,當你希望集群成為完全重新渲染.

                You can add a key prop to MarkerClusterGroup, and change it when you want the cluster to be completely rerendered.

                <MarkerClusterGroup
                  key={somekey}
                  spiderfyDistanceMultiplier={1}
                  showCoverageOnHover={false}
                >
                

                我不確定是什么邏輯觸發(fā)了標記的更改,但在相同的邏輯中,當標記更改時,會生成一個新的唯一值(無論是遞增的簡單計數(shù)器,還是新的 uuid),將 key 設(shè)置為該新值,整個組件應(yīng)卸載并重新安裝.雖然如果有大量標記,這對性能來說并不是很好,但這聽起來像是您正在嘗試做的事情.

                I'm not sure what logic is triggering the markers to change, but within that same logic, when the markers change, generate a new unique value (whether its a simple counter that increments, or a new uuid), set the key to that new value, and the entire component should unmount and remount. While this isn't great for performance if there are a huge number of markers, it sounds like what you're trying to do.

                這篇關(guān)于react-leaflet:在渲染新標記之前清除標記簇的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                相關(guān)文檔推薦

                Check if a polygon point is inside another in leaflet(檢查一個多邊形點是否在傳單中的另一個內(nèi)部)
                Changing leaflet markercluster icon color, inheriting the rest of the default CSS properties(更改傳單標記群集圖標顏色,繼承其余默認 CSS 屬性)
                Trigger click on leaflet marker(觸發(fā)點擊傳單標記)
                How can I change the default loading tile color in LeafletJS?(如何更改 LeafletJS 中的默認加載磁貼顏色?)
                Add external geojson to leaflet layer(將外部geojson添加到傳單層)
                Adding Leaflet layer control to sidebar(將 Leaflet 圖層控件添加到側(cè)邊欄)

                  <small id='NJ8Pe'></small><noframes id='NJ8Pe'>

                  <i id='NJ8Pe'><tr id='NJ8Pe'><dt id='NJ8Pe'><q id='NJ8Pe'><span id='NJ8Pe'><b id='NJ8Pe'><form id='NJ8Pe'><ins id='NJ8Pe'></ins><ul id='NJ8Pe'></ul><sub id='NJ8Pe'></sub></form><legend id='NJ8Pe'></legend><bdo id='NJ8Pe'><pre id='NJ8Pe'><center id='NJ8Pe'></center></pre></bdo></b><th id='NJ8Pe'></th></span></q></dt></tr></i><div class="uiiogma" id='NJ8Pe'><tfoot id='NJ8Pe'></tfoot><dl id='NJ8Pe'><fieldset id='NJ8Pe'></fieldset></dl></div>

                        • <bdo id='NJ8Pe'></bdo><ul id='NJ8Pe'></ul>
                          <tfoot id='NJ8Pe'></tfoot>
                            <tbody id='NJ8Pe'></tbody>

                          <legend id='NJ8Pe'><style id='NJ8Pe'><dir id='NJ8Pe'><q id='NJ8Pe'></q></dir></style></legend>
                          主站蜘蛛池模板: 高柔性拖链电缆_卷筒电缆_耐磨耐折聚氨酯电缆-玖泰特种电缆 | 福兰德PVC地板|PVC塑胶地板|PVC运动地板|PVC商用地板-中国弹性地板系统专业解决方案领先供应商! 福建成考网-福建成人高考网 | 北京浩云律师事务所-企业法律顾问_破产清算等公司法律服务 | 爆破器材运输车|烟花爆竹运输车|1-9类危险品厢式运输车|湖北江南专用特种汽车有限公司 | BAUER减速机|ROSSI-MERSEN熔断器-APTECH调压阀-上海爱泽工业设备有限公司 | CXB船用变压器-JCZ系列制动器-HH101船用铜质开关-上海永上船舶电器厂 | 品牌策划-品牌设计-济南之式传媒广告有限公司官网-提供品牌整合丨影视创意丨公关活动丨数字营销丨自媒体运营丨数字营销 | 河南橡胶接头厂家,河南波纹补偿器厂家,河南可曲挠橡胶软连接,河南套筒补偿器厂家-河南正大阀门 | YAGEO国巨电容|贴片电阻|电容价格|三星代理商-深圳市巨优电子有限公司 | ERP企业管理系统永久免费版_在线ERP系统_OA办公_云版软件官网 | 导电银胶_LED封装导电银胶_半导体封装导电胶厂家-上海腾烁 | SDG吸附剂,SDG酸气吸附剂,干式酸性气体吸收剂生产厂家,超过20年生产使用经验。 - 富莱尔环保设备公司(原名天津市武清县环保设备厂) | 储能预警-储能消防系统-电池舱自动灭火装置-四川千页科技股份有限公司官网 | 天津市能谱科技有限公司-专业的红外光谱仪_红外测油仪_紫外测油仪_红外制样附件_傅里叶红外光谱技术生产服务厂商 | 啤酒设备-小型啤酒设备-啤酒厂设备-济南中酿机械设备有限公司 | 大型低温冷却液循环泵-低温水槽冷阱「厂家品牌」京华仪器_京华仪器 | 合肥仿石砖_合肥pc砖厂家_合肥PC仿石砖_安徽旭坤建材有限公司 | 冷轧机|两肋冷轧机|扁钢冷轧机|倒立式拉丝机|钢筋拔丝机|收线机-巩义市华瑞重工机械制造有限公司 | 胶辊硫化罐_胶鞋硫化罐_硫化罐厂家-山东鑫泰鑫智能装备有限公司 意大利Frascold/富士豪压缩机_富士豪半封闭压缩机_富士豪活塞压缩机_富士豪螺杆压缩机 | 道康宁消泡剂-瓦克-大川进口消泡剂供应商 | 即用型透析袋,透析袋夹子,药敏纸片,L型涂布棒-上海桥星贸易有限公司 | 同步带轮_同步带_同步轮_iHF合发齿轮厂家-深圳市合发齿轮机械有限公司 | 依维柯自动挡房车,自行式国产改装房车,小型房车价格,中国十大房车品牌_南京拓锐斯特房车 - 南京拓锐斯特房车 | 超细|超微气流粉碎机|气流磨|气流分级机|粉体改性机|磨粉机|粉碎设备-山东埃尔派粉体科技 | 欧景装饰设计工程有限公司-无锡欧景装饰官网 | 皮带输送机-大倾角皮带输送机-皮带输送机厂家-河南坤威机械 | 光泽度计_测量显微镜_苏州压力仪_苏州扭力板手维修-苏州日升精密仪器有限公司 | 翻斗式矿车|固定式矿车|曲轨侧卸式矿车|梭式矿车|矿车配件-山东卓力矿车生产厂家 | 胶原检测试剂盒,弹性蛋白检测试剂盒,类克ELISA试剂盒,阿达木单抗ELISA试剂盒-北京群晓科苑生物技术有限公司 | 污泥烘干机-低温干化机-工业污泥烘干设备厂家-焦作市真节能环保设备科技有限公司 | 胜为光纤光缆_光纤跳线_单模尾纤_光纤收发器_ODF光纤配线架厂家直销_北京睿创胜为科技有限公司 - 北京睿创胜为科技有限公司 | 污水处理设备维修_污水处理工程改造_机械格栅_过滤设备_气浮设备_刮吸泥机_污泥浓缩罐_污水处理设备_污水处理工程-北京龙泉新禹科技有限公司 | 常州企业采购平台_常州MRO采购公司_常州米孚机电设备有限公司 | 减速机三参数组合探头|TSM803|壁挂式氧化锆分析仪探头-安徽鹏宸电气有限公司 | 焊锡,锡膏,锡线,锡条,焊锡膏-绿志岛金属有限公司 | 安徽千住锡膏_安徽阿尔法锡膏锡条_安徽唯特偶锡膏_卡夫特胶水-芜湖荣亮电子科技有限公司 | 小青瓦丨古建筑瓦丨青瓦厂家-宜兴市徽派古典建筑材料有限公司 | 高通量组织研磨仪-多样品组织研磨仪-全自动组织研磨仪-研磨者科技(广州)有限公司 | 干粉砂浆设备_干混砂浆生产线_腻子粉加工设备_石膏抹灰砂浆生产成套设备厂家_干粉混合设备_砂子烘干机--郑州铭将机械设备有限公司 | 膜结构_ETFE膜结构_膜结构厂家_膜结构设计-深圳市烨兴智能空间技术有限公司 | 上海租奔驰_上海租商务车_上海租车网-矢昂汽车服务公司 |