javascript - 谷歌地图不显示部分 View ?

标签 javascript jquery asp.net-mvc google-maps partial-views

我正在尝试在部分 View 中实现谷歌地图并尝试在页面上调用部分 View 这是我放置在主视图上的谷歌地图脚本

 <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDLhntIiMFhAoHu5XtrxDuh4EeNbm2ZuH0&libraries=places&callback=initAutocomplete" async defer></script>
    <script type="text/javascript">
        function initAutocomplete()
        {

            var map = new google.maps.Map(document.getElementById('map'), 
                {
                center: { lat: -33.8688, lng: 151.2195 },
                zoom: 13,
                mapTypeId: 'roadmap'
            });
            console.log('map', map);
            // 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);

            // Bias the SearchBox results towards current map's viewport.
            map.addListener('bounds_changed', function () {
                searchBox.setBounds(map.getBounds());
            });

            var markers = [];
            // Listen for the event fired when the user selects a prediction and retrieve
            // more details for that place.
            searchBox.addListener('places_changed', function () {
                var places = searchBox.getPlaces();

                if (places.length == 0) {
                    return;
                }

                // Clear out the old markers.
                markers.forEach(function (marker) {
                    marker.setMap(null);
                });
                markers = [];

                // For each place, get the icon, name and location.
                var bounds = new google.maps.LatLngBounds();
                places.forEach(function (place) {
                    if (!place.geometry) {
                        console.log("Returned place contains no geometry");
                        return;
                    }
                    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);
                    }
                    var address = $('#pac-input').val();
                    //document.getElementById('pac-input');
                    $.ajax({
                        url: "http://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&sensor=false",
                        type: "POST",
                        success: function (res) {
                            latitude = res.results[0].geometry.location.lat;
                            longitude = res.results[0].geometry.location.lng;
                        }
                    });
                });
                map.fitBounds(bounds);
            });
        }

这是我的 HTML,它放置在部分 View 中

 <div class="form-group">
            <label>Type a location to Search on Map</label>
            <input type="text" id="pac-input" class="form-control" ng-model="MyLocations.Location" required>
            <div class="selectlocatinonmap" id="map">
            </div>
    </div>

我已将此脚本放置在必须调用部分 View 的页面上,但谷歌地图无法处理部分 View 我也附上图片 请看一下 enter image description here

但是控制台没有显示任何错误。

最佳答案

可能是因为当调用生成 map 的回调函数时, map 容器被隐藏了。在淡入淡出或滑动效果完成后,您可以在单击显示表单的按钮时调用初始化 map 回调。

编辑

$('#my_button_wich_show_form').click(initAutocomplete);

请记住从 map URL 中删除回调参数。如果您在显示模态之前使用一些效果,请在效果完成后调用 initAutocomplete 函数。

在 initAutocomplete 函数中,您必须检查 #map 是否为空,以便可以执行脚本或仅触发 map 调整大小事件:

<script type="text/javascript">

    function initAutocomplete()
    {
        if( !$('#map').is(':empty') ){
            google.maps.event.trigger(this.map, 'resize');
            return false;
        }
        this.map = new google.maps.Map(document.getElementById('map'), 
            {
            center: { lat: -33.8688, lng: 151.2195 },
            zoom: 13,
            mapTypeId: 'roadmap'
        });
        var map = this.map;
        console.log('map', map);
        // 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);

        // Bias the SearchBox results towards current map's viewport.
        map.addListener('bounds_changed', function () {
            searchBox.setBounds(map.getBounds());
        });

        var markers = [];
        // Listen for the event fired when the user selects a prediction and retrieve
        // more details for that place.
        searchBox.addListener('places_changed', function () {
            var places = searchBox.getPlaces();

            if (places.length == 0) {
                return;
            }

            // Clear out the old markers.
            markers.forEach(function (marker) {
                marker.setMap(null);
            });
            markers = [];

            // For each place, get the icon, name and location.
            var bounds = new google.maps.LatLngBounds();
            places.forEach(function (place) {
                if (!place.geometry) {
                    console.log("Returned place contains no geometry");
                    return;
                }
                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);
                }
                var address = $('#pac-input').val();
                //document.getElementById('pac-input');
                $.ajax({
                    url: "http://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&sensor=false",
                    type: "POST",
                    success: function (res) {
                        latitude = res.results[0].geometry.location.lat;
                        longitude = res.results[0].geometry.location.lng;
                    }
                });
            });
            map.fitBounds(bounds);
        });
    }

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

相关文章:

javascript - 根据条件迭代数组对象

javascript - 如何只输入一个网站的链接?

javascript - 使用 fullpage.js 在部分之间滚动时如何固定文本动画更改,然后通过某个部分消失

c# - 是否可以将 ASP.NET 成员资格与在 ASP.NET Identity 2.0 中创建的表一起使用以进行身份​​验证?

c# - asp.net mvc View 页面可以有一个 webform 控件吗?

javascript - ASP.NET MVC 保存到客户端上的特定文件夹

javascript - 将 Javascript 替换为 Typescript

javascript - MONGOOSE - 仅显示我在对象内输入的特定值

javascript - Ember 绑定(bind) jQuery .val

javascript - jQuery.Deferred() 无法正常工作