javascript - Bootstrap 部分 View 模式中的 MVC 谷歌地图不起作用

标签 javascript jquery asp.net-mvc google-maps-api-3 bootstrap-modal

我正在做一个 MVC Bootstrap Google Map V3 应用程序。

当用户输入完整地址时,它会显示在Google map 中。如果显示的 div 位于 Partial View 中。看起来不错。

但是,当该部分 View 位于 Bootstrap 模态视图中时。它没有显示任何内容。

这是有效时的情况..

 <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=MyKey&sensor=false"></script>
 <script type="text/javascript">
   var geocoder;
    var map;
    var address = "Full Address ";
    function initialize() {
     
        geocoder = new google.maps.Geocoder();
        var latlng = new google.maps.LatLng(-34.397, 150.644);
        var myOptions = {
            zoom: 14,
            center: latlng,
     //       mapTypeControl: true,
         //   mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
       //     navigationControl: true,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById("dvMap"), myOptions);
        if (geocoder) {
            geocoder.geocode( { 'address': address}, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
                        map.setCenter(results[0].geometry.location);

                        var infowindow = new google.maps.InfoWindow(
                            { content: '<b>'+address+'</b>',
                                size: new google.maps.Size(150,50)
                            });

                        var marker = new google.maps.Marker({
                            position: results[0].geometry.location,
                            map: map, 
                            title:address
                        }); 
                        google.maps.event.addListener(marker, 'click', function() {
                            infowindow.open(map,marker);
                        });

                    } else {
                        alert("No results found");
                    }
                } else {
                    alert("Geocode was not successful for the following reason: " + status);
                }
            });
        }
 
    }


</script>

<div style="width: 600px; height:400px">
    @Html.Partial("~/Views/Shared/_ShowMap.cshtml")
  
</div>

这是对我的部分 View 的调用

enter image description here

在这种情况下我的部分 View 很简单

enter image description here

不起作用的情况是这样的。

我在 JavaScript 函数的 bootom 处添加了这一行...

 $("#myModalMapa").modal();

HTML Div是这样的。

enter image description here

我的部分 View 如下所示。

enter image description here

最佳答案

当我提出不同的问题时,我对脚本做了相同的更改

   $(document).ready(function () {
            $('#myModalMapa').on('shown.bs.modal', function () {
                var currentCenter = map.getCenter();  // Get current center before resizing
                google.maps.event.trigger(map, "resize");
                map.setCenter(currentCenter); // Re-set previous center
            });
        });

关于javascript - Bootstrap 部分 View 模式中的 MVC 谷歌地图不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42750874/

相关文章:

c# - 处理应用程序错误问题

javascript - MVC 中的 Kendo UI 网格页面大小

javascript - 防止删除最后一个子级 javascript

javascript - Array.fill(Array) 通过引用而不是值创建副本

javascript - 如何让复选框删除表单中的 'required' 验证

javascript - 从区域中删除 View 时会触发哪些 View 事件?

javascript - 循环 CSS3 动画

javascript - 使用 Selenium IDE 测试 jQuery show(), hide()

javascript - angular.js 更新模型

javascript - 如何将数据从 Controller 操作传递到 JavaScript 函数