Leaflet的popupclose函数没有按定义重置地图视图。

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

Leaflet popupclose fuction is not reseting the map view as defined

问题

我有一个Leaflet地图,用于可视化市中心地区的许多要素。我正在使用弹出窗口来可视化有关每个要素的附加信息。

当用户关闭弹出窗口时,我希望地图重新居中到原始位置和缩放级别。这应该使用popupclosesetview来支持。然而,这段代码似乎不起作用。我的脚本如下。

<script>

  var map = L.map('map', {minZoom: 15,}).setView([40.66,-73.6466], 17);

  L.tileLayer('http://basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}{r}.png', {
      attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
  }).addTo(map);

/* GeoJSON要素数据的开始 */

    var geojsonFeature = /* 帖子中省略了要素数据 */

/* GeoJSON要素数据的结束 */

        L.geoJSON(geojsonFeature, {
            onEachFeature: function(feature, layer) {
              var picture;
                if (feature.properties.picture_url) {
                  picture = '<img src="' + feature.properties.picture_url + '"/>';
                } else {
                  picture = '<img class="default-img" src="default_image.png" style="visibility:hidden;"/>';
                }
                var popup = L.popup({autoPan: true, keepInView: true, maxHeight: 500, autoPanPadding: [50, 50]})
                .setContent('<div class="popup_name">' + feature.properties.name + '</div>' + '<br>' + '<div class="popup_address">' + feature.properties.address + '</div>' + '<br>' + '<br>' + '<div class="popup_description">' + feature.properties.description + '</div>' + '<br>' + '<div class="popup_picture">' + picture + '</div>');
                
                layer.bindPopup(popup);
                
              }
            }).addTo(map);

        map.on('popupclose', function(e) {
            console.log("关闭弹出窗口!!!!");
            map.setView([40.66,-73.6466], 17);
            }

    </script>

地图可视化正常,弹出窗口也是正确的。但是当关闭弹出窗口时,地图没有重新居中。有人能告诉我为什么这不起作用吗?

请注意,我已经校正了您提供的HTML代码,以便更清晰地显示内容。如果您仍然遇到问题,您可能需要检查JavaScript代码是否正确,确保map.on('popupclose', function(e) {...}的括号放在正确的位置。

英文:

I have a Leaflet map that visualizes a bunch of features in a downtown area. I am using popups to visualize additional information about each feature.

When the user closes the popup, I want the map to re-center to the original position and zoom level. This should be supported using popupclose and setview. However, this code does not seem to be working. My script is below.

&lt;script&gt;
var map = L.map(&#39;map&#39;, {minZoom: 15,}).setView([40.66,-73.6466], 17);
L.tileLayer(&#39;http://basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}{r}.png&#39;, {
attribution: &#39;&amp;copy; &lt;a href=&quot;http://www.openstreetmap.org/copyright&quot;&gt;OpenStreetMap&lt;/a&gt;&#39;
}).addTo(map);
/* Beginning of Feature Data in GeoJSON */
var geojsonFeature = /* feature data omitted from post */
/* End of Feature Data in GeoJSON */
L.geoJSON(geojsonFeature, {
onEachFeature: function(feature, layer) {
var picture;
if (feature.properties.picture_url) {
picture = &#39;&lt;img src=&quot;&#39; + feature.properties.picture_url + &#39;&quot;/&gt;&#39;;
} else {
picture = &#39;&lt;img class=&quot;default-img&quot; src=&quot;default_image.png&quot; style=&quot;visibility:hidden;&quot;/&gt;&#39;;
}
var popup = L.popup({autoPan: true, keepInView: true, maxHeight: 500, autoPanPadding: [50, 50]})
.setContent(&#39;&lt;div class=&quot;popup_name&quot;&gt;&#39; + feature.properties.name + &#39;&lt;/div&gt;&#39; + &#39;&lt;br&gt;&#39; + &#39;&lt;div class=&quot;popup_address&quot;&gt;&#39; + feature.properties.address + &#39;&lt;/div&gt;&#39; + &#39;&lt;br&gt;&#39; + &#39;&lt;br&gt;&#39; + &#39;&lt;div class=&quot;popup_description&quot;&gt;&#39; + feature.properties.description + &#39;&lt;/div&gt;&#39; + &#39;&lt;br&gt;&#39; + &#39;&lt;div class=&quot;popup_picture&quot;&gt;&#39; + picture + &#39;&lt;/div&gt;&#39;);
layer.bindPopup(popup);
}
}).addTo(map);
map.on(&#39;popupclose&#39;), function(e) {
console.log(&quot;closing popup!!!!&quot;)
map.setView([40.66,-73.6466], 17);
}
&lt;/script&gt;
The the map is visualizing correctly and the popups are correct.  But the map is not re-centering when the popup is closed.  Can anyone tell me why this isn&#39;t working?
</details>
# 答案1
**得分**: 1
可能是因为有一个错误。
```javascript
map.on("popupclose", function (e) {
console.log("closing popup!!!!");
map.setView([40.66, -73.6466], 17);
});
英文:

Maybe because there is an error.

map.on(&#39;popupclose&#39;), function(e) {
console.log(&quot;closing popup!!!!&quot;)
map.setView([40.66,-73.6466], 17);
}

and it should be

map.on(&quot;popupclose&quot;, function (e) {
console.log(&quot;closing popup!!!!&quot;);
map.setView([40.66, -73.6466], 17);
});

huangapple
  • 本文由 发表于 2023年2月27日 01:10:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573667.html
匿名

发表评论

匿名网友

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

确定