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="8pan2u6" 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:在渲染新標(biāo)記之前清除標(biāo)記簇

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

          <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="j2ac8jc" 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:在渲染新標(biāo)記之前清除標(biāo)記簇的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                問題描述

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

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

                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}
                >
                


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


                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.

                地圖標(biāo)記和彈出窗口

                我選擇 Leaflet 和 react-leaflet 進(jìn)行地圖渲染,并選擇 react-leaflet-markercluster 來對(duì)彼此相鄰的標(biāo)記進(jìn)行分組.我有一個(gè)小問題:如果用戶按城市過濾相機(jī),然后他返回表單并決定按不同城市過濾,我想要發(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:

                • 清除上一個(gè)被過濾城市的所有標(biāo)記;
                • 顯示當(dāng)前城市的所有標(biāo)記;

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

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

                不同城市的標(biāo)記組

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

                這是我的 MapContainer.盡管存在我提到的錯(cuò)誤,但它運(yùn)行良好.

                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>
                

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

                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 包裝器來清除過去的城市標(biāo)記 如本答案所示.在向 MCG 添加新標(biāo)記之前,我確實(shí)執(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 組件.我也無法將相機(jī)對(duì)象保存在上下文中以在下一頁中進(jìn)一步使用它.所以我結(jié)束了堅(jiān)持我當(dāng)前的代碼,這是有效的,盡管有這個(gè)無法清除過去的城市標(biāo)記".錯(cuò)誤.

                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 時(shí)有一種方法可以清除過去的城市標(biāo)記.如果 MarkerClusterGroup 有一個(gè)鉤子,就像 Leaflet 地圖有一個(gè)鉤子一樣,我想這會(huì)更容易做到:我需要做的就是這樣簡(jiǎn)單的事情:

                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 中添加一個(gè) key 屬性,當(dāng)你希望集群成為完全重新渲染.

                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ā)了標(biāo)記的更改,但在相同的邏輯中,當(dāng)標(biāo)記更改時(shí),會(huì)生成一個(gè)新的唯一值(無論是遞增的簡(jiǎn)單計(jì)數(shù)器,還是新的 uuid),將 key 設(shè)置為該新值,整個(gè)組件應(yīng)卸載并重新安裝.雖然如果有大量標(biāo)記,這對(duì)性能來說并不是很好,但這聽起來像是您正在嘗試做的事情.

                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:在渲染新標(biāo)記之前清除標(biāo)記簇的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                相關(guān)文檔推薦

                Check if a polygon point is inside another in leaflet(檢查一個(gè)多邊形點(diǎn)是否在傳單中的另一個(gè)內(nèi)部)
                Changing leaflet markercluster icon color, inheriting the rest of the default CSS properties(更改傳單標(biāo)記群集圖標(biāo)顏色,繼承其余默認(rèn) CSS 屬性)
                Trigger click on leaflet marker(觸發(fā)點(diǎn)擊傳單標(biāo)記)
                How can I change the default loading tile color in LeafletJS?(如何更改 LeafletJS 中的默認(rèn)加載磁貼顏色?)
                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="gdcql8a" 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>
                          主站蜘蛛池模板: 原色会计-合肥注册公司_合肥代理记账公司_营业执照代办 | 聚丙烯酰胺PAM-聚合氯化铝PAC-絮凝剂-河南博旭环保科技有限公司 巨野电机维修-水泵维修-巨野县飞宇机电维修有限公司 | 中空玻璃生产线,玻璃加工设备,全自动封胶线,铝条折弯机,双组份打胶机,丁基胶/卧式/立式全自动涂布机,玻璃设备-山东昌盛数控设备有限公司 | 自动检重秤-动态称重机-重量分选秤-苏州金钻称重设备系统开发有限公司 | 企典软件一站式企业管理平台,可私有、本地化部署!在线CRM客户关系管理系统|移动办公OA管理系统|HR人事管理系统|人力 | 注塑模具_塑料模具_塑胶模具_范仕达【官网】_东莞模具设计与制造加工厂家 | 衬氟止回阀_衬氟闸阀_衬氟三通球阀_衬四氟阀门_衬氟阀门厂-浙江利尔多阀门有限公司 | 进口消泡剂-道康宁消泡剂-陶氏消泡剂-大洋消泡剂 | 专业生物有机肥造粒机,粉状有机肥生产线,槽式翻堆机厂家-郑州华之强重工科技有限公司 | 时代北利离心机,实验室离心机,医用离心机,低速离心机DT5-2,美国SKC采样泵-上海京工实业有限公司 工业电炉,台车式电炉_厂家-淄博申华工业电炉有限公司 | 多功能真空滤油机_润滑油全自动滤油机_高效真空滤油机价格-重庆润华通驰 | 小程序开发公司_APP开发多少钱_软件开发定制_微信小程序制作_客户销售管理软件-济南小溪畅流网络科技有限公司 | 双相钢_双相不锈钢_双相钢圆钢棒_双相不锈钢报价「海新双相钢」 双能x射线骨密度检测仪_dxa骨密度仪_双能x线骨密度仪_品牌厂家【品源医疗】 | 消防泵-XBD单级卧式/立式消防泵-上海塑泉泵阀(集团)有限公司 | MOOG伺服阀维修,ATOS比例流量阀维修,伺服阀维修-上海纽顿液压设备有限公司 | 抖音短视频运营_企业网站建设_网络推广_全网自媒体营销-东莞市凌天信息科技有限公司 | 恒温恒湿试验箱_高低温试验箱_恒温恒湿箱-东莞市高天试验设备有限公司 | 北京森语科技有限公司-模型制作专家-展览展示-沙盘模型设计制作-多媒体模型软硬件开发-三维地理信息交互沙盘 | 浙江筋膜枪-按摩仪厂家-制造商-肩颈按摩仪哪家好-温州市合喜电子科技有限公司 | 电磁铁_推拉电磁铁_机械手电磁吸盘电磁铁厂家-广州思德隆电子公司 | 上海防爆真空干燥箱-上海防爆冷库-上海防爆冷柜?-上海浦下防爆设备厂家? | 专业生物有机肥造粒机,粉状有机肥生产线,槽式翻堆机厂家-郑州华之强重工科技有限公司 | 喷码机,激光喷码打码机,鸡蛋打码机,手持打码机,自动喷码机,一物一码防伪溯源-恒欣瑞达有限公司 | 二手注塑机回收_旧注塑机回收_二手注塑机买卖 - 大鑫二手注塑机 二手光谱仪维修-德国OBLF光谱仪|进口斯派克光谱仪-热电ARL光谱仪-意大利GNR光谱仪-永晖检测 | 南京试剂|化学试剂|分析试剂|实验试剂|cas号查询-专业60年试剂销售企业 | 无尘烘箱_洁净烤箱_真空无氧烤箱_半导体烤箱_电子防潮柜-深圳市怡和兴机电 | 橡胶接头|可曲挠橡胶接头|橡胶软接头安装使用教程-上海松夏官方网站 | 兰州UPS电源,兰州山特UPS-兰州万胜商贸| 有机废气处理-rto焚烧炉-催化燃烧设备-VOC冷凝回收装置-三梯环境 | 防火板_饰面耐火板价格、厂家_品牌认准格林雅| 高低温老化试验机-步入式/低温恒温恒湿试验机-百科 | 河南中专学校|职高|技校招生-河南中职中专网 | 长春网站建设,五合一网站设计制作,免费优化推广-长春网站建设 | 找培训机构_找学习课程_励普教育 | 在线钠离子分析仪-硅酸根离子浓度测定仪-油液水分测定仪价格-北京时代新维测控设备有限公司 | 临时厕所租赁_玻璃钢厕所租赁_蹲式|坐式厕所出租-北京慧海通 | 假肢-假肢价格-假肢厂家-河南假肢-郑州市力康假肢矫形器有限公司 | 铸铝门厂家,别墅大门庭院大门,别墅铸铝门铜门[十大品牌厂家]军强门业 | OLChemim试剂-ABsciex耗材-广州市自力色谱科仪有限公司 | EDLC超级法拉电容器_LIC锂离子超级电容_超级电容模组_软包单体电容电池_轴向薄膜电力电容器_深圳佳名兴电容有限公司_JMX专注中高端品牌电容生产厂家 | 耐腐蚀泵,耐腐蚀真空泵,玻璃钢真空泵-淄博华舜耐腐蚀真空泵有限公司 |