javascript - 在同一页面上显示 3 个传单 map

标签 javascript php leaflet

我遵循了许多关于在同一页面上显示两个传单 map 的教程。我的最终目标是拥有一个 php 文件,我可以将地址传递给它,它会返回一个带有 map 的 div,但现在我只是想让 3 个 map 出现在同一页面上。它可以与两个一起使用,但不能与三个一起使用,我要秃头试图解决这个问题! 关于为什么会显示前两张 map 而不是第三张 map ,有什么想法吗?

<div class="col-md-6 col-sm-12">
<div class="map-wrapper">
<div id="map_canvas"></div>
</div>
</div>

<div class="col-md-6 col-sm-12">
<div class="map-wrapper">
<div id="map_canvas2"></div>
</div>
</div>
<div class="col-md-6 col-sm-12">
<div class="map-wrapper">
<div id="map_canvas3"></div>
</div>
</div>

<script>
var firefoxIcon = L.icon({
iconUrl: 'pin2.png',
iconAnchor: [14, 48]
});

var map = L.map('map_canvas').setView([40.744996, -73.983761], 16);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png').addTo(map);
var marker = L.marker([40.744996, -73.983761], {icon: firefoxIcon}).addTo(map);

var map2 = L.map('map_canvas2').setView([38.895856, -77.009787], 16);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png').addTo(map2);
var marker = L.marker([38.895856, -77.009787], {icon: firefoxIcon}).addTo(map2);

var map3 = L.map('map_canvas3').setView([40.870100, -73.458890], 16);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png').addTo(map3);
var marker = L.marker([40.870100, -73.458890], {icon: firefoxIcon}).addTo(map3);
</script>

最佳答案

试试这个。将类“map”添加到“map_canvas”元素。

<div class="col-md-6 col-sm-12">
<div class="map-wrapper">
<div class="map" id="map_canvas"></div>
</div>
</div>

<div class="col-md-6 col-sm-12">
<div class="map-wrapper">
<div class="map" id="map_canvas2"></div>
</div>
</div>
<div class="col-md-6 col-sm-12">
<div class="map-wrapper">
<div class="map" id="map_canvas3"></div>
</div>
</div>

<script>
var firefoxIcon = L.icon({
iconUrl: 'pin2.png',
iconAnchor: [14, 48]
});

var map = L.map('map_canvas').setView([40.744996, -73.983761], 16);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png').addTo(map);
var marker = L.marker([40.744996, -73.983761], {icon: firefoxIcon}).addTo(map);

var map2 = L.map('map_canvas2').setView([38.895856, -77.009787], 16);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png').addTo(map2);
var marker = L.marker([38.895856, -77.009787], {icon: firefoxIcon}).addTo(map2);

var map3 = L.map('map_canvas3').setView([40.870100, -73.458890], 16);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png').addTo(map3);
var marker = L.marker([40.870100, -73.458890], {icon: firefoxIcon}).addTo(map3);
</script>

然后在您的 css 文件中添加此样式:

.map {
    height: 500px;
    width: 80%;
    padding: 10px
}

它会起作用的。在 jfiddle 中测试。

关于javascript - 在同一页面上显示 3 个传单 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52597399/

相关文章:

javascript - 创建传单标记并通过循环对象列表将它们存储在变量中

php - 如何在 codeigniter 中将数据从 post 传递到 ajax

javascript - 保存leaflet.js的当前 View

javascript - React 中复选框 Sum 的组件

javascript - 动态添加的行不是可视化的 HTML

javascript - 如何在移动版wordpress上启用swf文件自动播放

php - 我们如何检查我们是否在本地主机上运行 PHP 脚本?

javascript - React-Leaflet 通过状态变化改变 GEOJSON 形状颜色

javascript - 渲染多个独立的 redux-form

javascript - 如何将函数声明和 new 运算符组合到一条语句中