javascript 具有相同选项的同一页面上的多个传单地图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25293579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 04:18:35  来源:igfitidea点击:

Multiple Leaflet maps on same page with same options

javascriptopenlayersleaflet

提问by Dmitry

I'm pretty new to leaflet.js and I'm trying to figure out how to assign the same map with the same set of options and layers to a different HTML container instead of having to remove and add a new one every time?

我是 Leaflet.js 的新手,我想弄清楚如何将具有相同选项和图层集的相同地图分配给不同的 HTML 容器,而不是每次都删除和添加一个新的容器?

I used to deal with Open layers 2.13 and I had map.render(div); option every time I wanted to set map to another div. Is there a solution similar to this? Many thanks!

我曾经处理过 Open layer 2.13 并且我有 map.render(div); 每次我想将地图设置为另一个 div 时的选项。有没有类似的解决方案?非常感谢!

回答by YaFred

You can, but you have to duplicate the layers

你可以,但你必须复制图层

// add an OpenStreetMap tile layer
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
    attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);

// add the same OpenStreetMap tile layer to the second map
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
    attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map2);

http://jsfiddle.net/FranceImage/aj44r7v2/

http://jsfiddle.net/FranceImage/aj44r7v2/