首页 > 解决方案 > 传单地图的引导 4 模态问题

问题描述

当我在引导程序 4 模式中使用传单地图时,我遇到了问题。

地图无法正确渲染,我不知道如何修复它。

这是我的地图脚本:

var url = 'http://{s}.tile.osm.org/{z}/{x}/{y}.png';
var attr = '&copy; <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> contributors',
    osm = L.tileLayer(url, {
        maxZoom: 18,
        attribution: attr
    });
var map = L.map('mapId').setView([35.6892, 51.3890], 10).addLayer(osm);
map.on('click', onMapClick);
var markers = [];
function onMapClick(e) {
    var geojsonFeature = {
        "type": "Feature",
        "properties": {},
        "geometry": {
            "type": "Point",
            "coordinates": [e.latlng.lat, e.latlng.lng]
        }
    }
    if (markers.length > 0) {
        map.removeLayer(markers.pop());
    }
    var marker;
    document.getElementById('latitude').value = e.latlng.lat;
    document.getElementById('longitude').value = e.latlng.lng;
    L.geoJson(geojsonFeature, {
        pointToLayer: function (feature, latlng) {
            marker = L.marker(e.latlng, {
                riseOnHover: true,
                draggable: true,
            });
            markers.push(marker);
            return marker;
        }
    }).addTo(map);
}

我正在寻找解决方案来解决它::

这是我的引导模式代码:

<div class="modal fade" id="mapModal" tabindex="-1" role="dialog" aria-labelledby="mapModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="mapModalLabel">انتخاب موقعیت مکانی ملک از روی نقشه:</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <div id="mapId"></div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-danger btn-sm">ثبت</button>
            </div>
        </div>
    </div>
</div>

标签: javascriptleafletbootstrap-modal

解决方案


我找到了:

<script>
$('#mapModal').on('shown.bs.modal', function(){
    setTimeout(function() {
        map.invalidateSize();
    }, 10);
});


推荐阅读