【问题标题】:Google Map doesn't load second time in JQuery UI dialog谷歌地图不会在 JQuery UI 对话框中第二次加载
【发布时间】:2015-07-21 07:22:06
【问题描述】:

在我的 ASP.Net 应用程序的按钮的 OnClientClick 中,我调用此函数来显示一个带有 Google Map Inside 的 JQueryUI 对话框。一切都是第一次运行良好。但是,第二次,地图似乎是灰色背景的空白,但是,地图内的所有其他控件都显示出来了。这是我的代码。有什么方法可以在关闭时处理地图,以便我可以在单击下一个按钮时再次重新加载地图。

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script>
function loadMap()
{

       var currentMapPosition = new google.maps.LatLng($('#<%=txLatitude.ClientID %>').val(), $('#<%= txLongitude.ClientID %>').val()); 
       var mapOptions = {
            center: currentMapPosition,
            zoom: 14,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var infoWindow = new google.maps.InfoWindow();
        var latlngbounds = new google.maps.LatLngBounds();
        var map = new google.maps.Map(document.getElementById("gmap-dialog"), mapOptions);

        var marker = new google.maps.Marker({
            position: currentMapPosition,
            map: map,
            title: 'SubProject Location'
        });
       //collects New Location, and closing dialog on click
        google.maps.event.addListener(map, 'click', function (e) {
            var latitude = e.latLng.lat();
            var longitude = e.latLng.lng();

                  var marker = new google.maps.Marker({
                    position: e.latLng,
                    map: map,
                    title: 'SubProject Location'
                });
                // Center of map
                map.panTo(new google.maps.LatLng(latitude,longitude));
                $('#<%= txLatitude.ClientID %>').val(e.latLng.lat() ) ;
                $('#<%= txLongitude.ClientID %>').val(e.latLng.lng() ) ;

               $("#gmap-dialog").dialog('close');
        });

          $("#gmap-dialog").dialog('open');
}
</script>

问题与live demo 相同。 如果您第二次加载地图,它将是灰色的。我在 Chrome、IE 和 Firefox 中检查过。

【问题讨论】:

  • 第二次是什么意思?你在隐藏地图吗?如果是这样,当您再次显示地图时,您将需要 google.maps.event.trigger(map, "resize");
  • 是的...它在对话框中。用户可以关闭对话框,也可以点击地图选择位置
  • 调整大小没有帮助。它又是一样的。 @kaho
  • 问题和这个现场演示一样。 wecodeyoursite.com/samples/GoogleMaps/…如果你第二次加载地图,它会是灰色的。我正在使用 Chrome。

标签: jquery google-maps jquery-ui


【解决方案1】:

我找到了答案。问题是多次初始化地图对象。我全局声明了 map 变量,并且只使用条件初始化了一次,现在它可以正常工作了。

 var map = ''; 
 function loadMap()
 {

       var currentMapPosition = new google.maps.LatLng($('#<%=txLatitude.ClientID %>').val(), $('#<%= txLongitude.ClientID %>').val()); 
       var mapOptions = {
            center: currentMapPosition,
            zoom: 14,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var infoWindow = new google.maps.InfoWindow();
        var latlngbounds = new google.maps.LatLngBounds();
        if(!map){
            map = new google.maps.Map(document.getElementById("gmap-dialog"), mapOptions);
        }
        else{
            map.setOptions(mapOptions);
        }

        var marker = new google.maps.Marker({
            position: currentMapPosition,
            map: map,
            title: 'SubProject Location'
        });

        google.maps.event.addListener(map, 'click', function (e) {
            var latitude = e.latLng.lat();
            var longitude = e.latLng.lng();

                  var marker = new google.maps.Marker({
                    position: e.latLng,
                    map: map,
                    title: 'SubProject Location'
                });
                // Center of map
                map.panTo(new google.maps.LatLng(latitude,longitude));
                $('#<%= txLatitude.ClientID %>').val(e.latLng.lat() ).attr('class', 'modified-textbox');
                $('#<%= txLongitude.ClientID %>').val(e.latLng.lng() ).attr('class', 'modified-textbox');
                  $('#<%= saveButton.ClientID %>').attr('class', 'save-button');

               $("#gmap-dialog").dialog('close');
        });

          $("#gmap-dialog").dialog('open');
 }

【讨论】:

  • 谢谢摩西,你节省了我很多时间:)
猜你喜欢
  • 1970-01-01
  • 2014-12-25
  • 1970-01-01
  • 1970-01-01
  • 2020-09-12
  • 1970-01-01
  • 2015-12-23
  • 2011-08-31
  • 1970-01-01
相关资源
最近更新 更多