我的leaflet地图在折叠左侧面板时显示空白空间。

huangapple go评论188阅读模式
英文:

My leaflet map is showing blank space while collapsing the left panel

问题

以下是您要翻译的部分:

I'm creating a portal, where am using leaflet map. But when I collapsing my left panel. it showing blank space. how to solve it. Also I have invalidateSize() function . but It's not working. Kindly let me know how to solve it.

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>

         <link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.3/dist/leaflet.css"
         integrity="sha256-kLaT2GOSpHechhsozzB+flnD+zUyjE2LlfWPgU04xyI="
         crossorigin=""/>
     
        <!-- Make sure you put this AFTER Leaflet's CSS -->
     <script src="https://unpkg.com/leaflet@1.9.3/dist/leaflet.js"
         integrity="sha256-WBkoXOwTeyKclOHuWtc+i2uENFpDZ9YPdf5Hf+D7ewM="
         crossorigin=""></script>
    
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
    
    
        <style>
            #map { height: 100%; }

            .box {
            }

            .left {
               width:20%;
               background:red;
               float:left;
            }

            .right {
                width:80%;
                background:green;
                 float:left;
            }

            .right-100 {
                 width:100%;
            }

            .left-0 {
                 width:0%;
            }

            .collapse {
                background:blue;
                color:white;
                width:100px;
            }
        </style>
    </head>
    <body>


        <div class="collapse" id="collapse">collape it</div>
        <div class="box">
            <div class="left">
                Hello Shubham
            </div>

            <div class="right ">
                 <div id="map"></div>
            </div>
        </div>

        <script>
            var map = L.map('map').setView([51.505, -0.09], 13);

            L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
                attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
                maxZoom: 18
            }).addTo(map);

            // Listen for the event that fires when the panel is collapsed
            document.getElementById("#collapse").addEventListener("click", function () {
                alert('sasASas');
                // Call the invalidateSize method to trigger a recalculation of the map's size
                map.invalidateSize();
            });
        </script>

        <script>
            $(document).on('click', '.collapse', function () {
                alert('SasASas');
                $('.right').addClass('right-100');
                $('.left').addClass('left-0')
            })
        </script>
    </body>
</html>
英文:

I'm creating a portal, where am using leaflet map. But when I collapsing my left panel. it showing blank space. how to solve it. Also I have invalidateSize() function . but It's not working. Kindly let me know how to solve it.

&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
    &lt;head&gt;
        &lt;title&gt;&lt;/title&gt;
    
    
         &lt;link rel=&quot;stylesheet&quot; href=&quot;https://unpkg.com/leaflet@1.9.3/dist/leaflet.css&quot;
         integrity=&quot;sha256-kLaT2GOSpHechhsozzB+flnD+zUyjE2LlfWPgU04xyI=&quot;
         crossorigin=&quot;&quot;/&gt;
    
        &lt;!-- Make sure you put this AFTER Leaflet&#39;s CSS --&gt;
     &lt;script src=&quot;https://unpkg.com/leaflet@1.9.3/dist/leaflet.js&quot;
         integrity=&quot;sha256-WBkoXOwTeyKclOHuWtc+i2uENFpDZ9YPdf5Hf+D7ewM=&quot;
         crossorigin=&quot;&quot;&gt;&lt;/script&gt;
    
        &lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js&quot;&gt;&lt;/script&gt;
    
    
        &lt;style&gt;
            #map { height: 100%; }
    
            .box {
            }
    
            .left {
               width:20%;
               background:red;
               float:left;
            }
    
            .right {
                width:80%;
                background:green;
                 float:left;
            }
    
            .right-100 {
                 width:100%;
            }
    
            .left-0 {
                 width:0%;
            }
    
            .collapse {
                background:blue;
                color:white;
                width:100px;
            }
        &lt;/style&gt;
    &lt;/head&gt;
    &lt;body&gt;
    
    
        &lt;div class=&quot;collapse&quot; id=&quot;collapse&quot;&gt;collape it&lt;/div&gt;
        &lt;div class=&quot;box&quot;&gt;
            &lt;div class=&quot;left&quot;&gt;
                Hello Shubham
            &lt;/div&gt;
    
            &lt;div class=&quot;right &quot;&gt;
                 &lt;div id=&quot;map&quot;&gt;&lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    
    
    
    
    
    
    
    
    
        &lt;script&gt;
            var map = L.map(&#39;map&#39;).setView([51.505, -0.09], 13);
    
            L.tileLayer(&#39;https://tile.openstreetmap.org/{z}/{x}/{y}.png&#39;, {
                attribution: &#39;&amp;copy; &lt;a href=&quot;http://www.openstreetmap.org/copyright&quot;&gt;OpenStreetMap&lt;/a&gt;&#39;,
                maxZoom: 18
            }).addTo(map);
    
            // Listen for the event that fires when the panel is collapsed
            document.getElementById(&quot;#collapse&quot;).addEventListener(&quot;click&quot;, function () {
                alert(&#39;sasASas&#39;);
                // Call the invalidateSize method to trigger a recalculation of the map&#39;s size
                map.invalidateSize();
            });
            
    
    
    
    
    
        &lt;/script&gt;
    
        &lt;script&gt;
            $(document).on(&#39;click&#39;, &#39;.collapse&#39;, function () {
                alert(&#39;SasASas&#39;);
                $(&#39;.right&#39;).addClass(&#39;right-100&#39;);
                $(&#39;.left&#39;).addClass(&#39;left-0&#39;)
            })
        &lt;/script&gt;
    
    &lt;/body&gt;
    &lt;/html&gt;

答案1

得分: 0

请尝试这个,我希望它会起作用,只需在您的文档中的点击函数中添加map.invalidateSize();

&lt;script&gt;
$(document).on(&#39;click&#39;, &#39;.collapse&#39;, function () {
    alert(&#39;SasASas&#39;);
    $(&#39;.right&#39;).addClass(&#39;right-100&#39;);
    $(&#39;.left&#39;).addClass(&#39;left-0&#39;)
    map.invalidateSize();
})
&lt;/script&gt;

如果您需要更多帮助,请告诉我。

英文:

Please try this, i hope it will work, just added map.invalidateSize(); in your document on click function.

 &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
	&lt;title&gt;&lt;/title&gt;


	 &lt;link rel=&quot;stylesheet&quot; href=&quot;https://unpkg.com/leaflet@1.9.3/dist/leaflet.css&quot;
	 integrity=&quot;sha256-kLaT2GOSpHechhsozzB+flnD+zUyjE2LlfWPgU04xyI=&quot;
	 crossorigin=&quot;&quot;/&gt;

	&lt;!-- Make sure you put this AFTER Leaflet&#39;s CSS --&gt;
 &lt;script src=&quot;https://unpkg.com/leaflet@1.9.3/dist/leaflet.js&quot;
	 integrity=&quot;sha256-WBkoXOwTeyKclOHuWtc+i2uENFpDZ9YPdf5Hf+D7ewM=&quot;
	 crossorigin=&quot;&quot;&gt;&lt;/script&gt;

	&lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js&quot;&gt;&lt;/script&gt;


	&lt;style&gt;
		#map { height: 100%; }

		.box {
		}

		.left {
		   width:20%;
		   background:red;
		   float:left;
		}

		.right {
			width:80%;
			background:green;
			 float:left;
		}

		.right-100 {
			 width:100%;
		}

		.left-0 {
			 width:0%;
		}

		.collapse {
			background:blue;
			color:white;
			width:100px;
		}
	&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;


	&lt;div class=&quot;collapse&quot; id=&quot;collapse&quot;&gt;collape it&lt;/div&gt;
	&lt;div class=&quot;box&quot;&gt;
		&lt;div class=&quot;left&quot;&gt;
			Hello Shubham
		&lt;/div&gt;

		&lt;div class=&quot;right &quot;&gt;
			 &lt;div id=&quot;map&quot;&gt;&lt;/div&gt;
		&lt;/div&gt;
	&lt;/div&gt;









	&lt;script&gt;
		var map = L.map(&#39;map&#39;).setView([51.505, -0.09], 13);

		L.tileLayer(&#39;https://tile.openstreetmap.org/{z}/{x}/{y}.png&#39;, {
			attribution: &#39;&amp;copy; &lt;a href=&quot;http://www.openstreetmap.org/copyright&quot;&gt;OpenStreetMap&lt;/a&gt;&#39;,
			maxZoom: 18
		}).addTo(map);

	&lt;/script&gt;

	&lt;script&gt;
		$(document).on(&#39;click&#39;, &#39;.collapse&#39;, function () {
			alert(&#39;SasASas&#39;);
			$(&#39;.right&#39;).addClass(&#39;right-100&#39;);
			$(&#39;.left&#39;).addClass(&#39;left-0&#39;)
			map.invalidateSize();
		})
	&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;

huangapple
  • 本文由 发表于 2023年2月6日 14:54:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75358184.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定