javascript - 谷歌地图不显示

标签 javascript jquery google-maps fadein symfony-3.2

我正在使用 Symfony 3 开发一个应用程序。我有一个 div,其中显示了 Google map 。

我正在隐藏页面

<div id="page_content" style="display: none">
</div>

然后我这样做了:

$(document).ready(function () {
    $('#page_content').fadeIn('slow');
})

This let me show a little animation while the browser is creating the page's doms.
But in this way the div in which there is the map is displayed only if I press the <kbd>F12</kbd> key. 

The div in which I'm displaying map: 

```html
<div class="md-card-content large-padding">
    <div id="displaymap">
        <div>
            <input id="pac-input" class="controls" type="text" placeholder="Search Box">
            <div id="map-canvas" style="width: 100%; height: 375px;">
            </div>
        </div>
    </div>
</div>

JavaScript 函数:

var init_lng = '{{ bien.longitude }}';
    var init_lat = '{{ bien.latitude }}';
    var marker;
    googleMapApiLaunch(init_lng, init_lat);
    function googleMapApiLaunch(lng, lat) {
        var longitude = $('#sbc_bienbundle_bien_longitude');
        var latitude = $('#sbc_bienbundle_bien_latitude');
        {% if bien.id != 0 %}
        longitude.val(lng);
        latitude.val(lat);
        {% else %}
        if (lat === init_lat && lng === init_lng) {
            longitude.val('');
            latitude.val('');
        } else {
            longitude.val(lng);
            latitude.val(lat);
        }
        {% endif %}
        lat = parseFloat(lat);
        lng = parseFloat(lng);
        //map..
        var map = new google.maps.Map(document.getElementById('map-canvas'), {
            center: {
                lat: lat,
                lng: lng
            },
            zoom: 14
        });
        // Create the search box and link it to the UI element.
        var input = document.getElementById('pac-input');
        var searchBox = new google.maps.places.SearchBox(input);
        map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
        var markers = [];
        // Bias the SearchBox results towards current map's viewport.
        google.maps.event.addListener(searchBox, 'places_changed', function () {
            var places = searchBox.getPlaces();
            if (places.length === 0) {
                return;
            }
            // Clear out the old markers.
            markers.forEach(function (iMarker) {
                iMarker.setMap(null);
            });
            markers = [];
            // For each place, get the icon, name and location.
            var bounds = new google.maps.LatLngBounds();
            places.forEach(function (place) {
                var icon = {
                    url: place.icon,
                    size: new google.maps.Size(71, 71),
                    origin: new google.maps.Point(0, 0),
                    anchor: new google.maps.Point(17, 34),
                    scaledSize: new google.maps.Size(25, 25)
                };
                // Create a marker for each place.
                markers.push(new google.maps.Marker({
                    map: map,
                    icon: icon,
                    title: place.name,
                    position: place.geometry.location
                }));
                if (place.geometry.viewport) {
                    // Only geocodes have viewport.
                    bounds.union(place.geometry.viewport);
                } else {
                    bounds.extend(place.geometry.location);
                }
            });
            map.fitBounds(bounds);
        });
        //marker..
        marker = new google.maps.Marker({
            position: {
                lat: lat,
                lng: lng
            },
            map: map,
            draggable: true
        });
        //dragend event of maker
        google.maps.event.addListener(marker, 'dragend', function () {
            //console.log(marker.getPosition());
            if (ismapped.is(':checked')) {
                longitude.val(marker.getPosition().lng());
                latitude.val(marker.getPosition().lat());
            }
        });
    }

我需要做什么才能让 map 显示而不删除 fadeIn('slow') 动画?

最佳答案

您立即调用 googleMapApiLaunch...只需在 fadeIn 完成时执行即可:

$(document).ready(function () {
    $('#page_content').fadeIn('slow', function() {
        googleMapApiLaunch(init_lng, init_lat);
    );
});

更新:因为您无法直接从主 fadeIn 方法调用 googleMapApiLaunch,所以您可以从那里触发一个事件。然后有一个事件监听器,仅在 map 页面上使用。例如

$(document).ready(function () {
    $('#page_content').fadeIn('slow', function() {
        $(this).trigger("fadeInComplete");
    );
});

然后在你的 map 页面JS中:

$('#page_content').on('fadeInComplete', function () {
    googleMapApiLaunch(init_lng, init_lat);
});

关于javascript - 谷歌地图不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44002003/

相关文章:

javascript - 如何将外部 JS 函数与 HTML 按钮单击链接

javascript - 如何在一个循环而不是两个循环中向数组添加唯一的 id?

javascript - knockout : Function Scope Error

javascript - 显示模态时执行功能

javascript - 用我的 json 数据填充 JQuery DataTable

jquery - 动画一个 div 标签,并在 cearten 时间后使用 jquery 隐藏它

javascript - 单击事件始终使用最后一个标记

javascript - 接收本地语言的 Google 自动完成结果

javascript - 如何防止自定义光标移动字母?

javascript - 实时数据更新(车辆跟踪)