Google地图未在部分视图中显示?

时间:2017-06-08 06:02:42

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

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

 <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放置在局部视图中

 <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>

我已将此脚本放在我必须调用部分视图的页面上,但谷歌地图无法在局部视图上工作 我也附上了这张照片 请看一下 enter image description here

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

2 个答案:

答案 0 :(得分:1)

也许是因为当调用生成地图的回调函数时,地图容器会被隐藏。在完成淡入淡出或滑动效果后,您可以在显示表单的按钮上调用init map回调。

修改

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

请记住从地图网址中删除回调参数。如果在显示模态之前使用某些效果,请在效果完成后调用initAutocomplete函数。

在initAutocomplete函数中,您必须检查#map是否为空,这样您就可以执行脚本或只触发map resize事件:

<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);
        });
    }

答案 1 :(得分:0)

运行Fiddler它是免费的,看看你的请求是否正在进行googgle。根据您的屏幕截图,我没有看到任何xhr到Google地图的API。我想你被封锁了。