谷歌地图不呈现在模态弹出

谷歌地图不呈现在模态弹出

问题描述:

以下是我的jQuery代码,我使用部分视图渲染谷歌地图上的模式弹出。谷歌地图不呈现在模态弹出

地图不呈现本身,它呈现如果我调整浏览器窗口的大小或当我检查我的浏览器(窗口调整大小相同的东西)。

此外,当它呈现不在中心根据纬度经度设置。

我想根据从隐藏字段设置的纬度经度动态呈现地图。目前我通过纬度= 30.704648600和经度= 76.717872600。

我不知道什么是失踪请帮助,TIA。

的jQuery: -

$(document).ready(function() { 
     var currLoc; 
     if ($('#Latitude').val().length > 0 && $('#Longitude').val().length > 0) 
      initialize($('#Latitude').val(), $('#Longitude').val()); 
    }); 

    function initialize(Latitude, Longitude) { 
     var latlng = new google.maps.LatLng(Latitude, Longitude); 
     var map = new google.maps.Map(document.getElementById('map_'), { 
      center: latlng, 
      zoom: 13 
     }); 
     var marker = new google.maps.Marker({ 
      map: map, 
      position: latlng, 
      draggable: false, 
      //anchorPoint: new google.maps.Point(0, -29) 
     }); 
     map.setCenter(new google.maps.LatLng(Latitude, Longitude)); 

     var infowindow = new google.maps.InfoWindow(); 
     currLoc= $('#lastLocation').text(); 
     google.maps.event.addListener(marker, 'click', function() { 
      var iwContent = '<div id="iw_container">' + 
      '<div class="iw_title"><b>Last Location</b> : <span class="lastLoc">' + GetAddress(Latitude, Longitude) + '<span></div></div>';    
      infowindow.setContent(iwContent);    
      infowindow.open(map, marker); 
     }); 
    } 

    function GetAddress(Latitude, Longitude) { 
     var LastLatLong = []; 
     LastLatLong.push([Latitude, Longitude]); 
     var latlngArray_ = LastLatLong[0]; 
     var latlngset_ = new google.maps.LatLng(latlngArray_[0], latlngArray_[1]); 
     var geocoder = new google.maps.Geocoder(); 
     geocoder.geocode({ 'latLng': latlngset_ }, function (results, status) { 
      if (status == google.maps.GeocoderStatus.OK) { 
       if (results[0]) { 
        //currLoc= results[0].formatted_address; 
        if ($('.lastLoc').length > 0) { 
         debugger; 
         $('.lastLoc').text(results[0].formatted_address); 
        } 
       } 
      } 
     }); 
    } 
+1

当div没有大小(它没有显示任何内容)时,您可能正在初始化您的地图。在弹出窗口打开时初始化它,或者在弹出窗口打开时启动 - 在页面加载 - 并在初始化后隐藏它。为此,coukld在开始时使用不透明度0,并在隐藏显示none后将其更改为1。 –

我得到了答案,也可以通过先触发地图的resize事件做了,那么使用它的setCenter属性。

我改变了我的初始化函数现在它的工作正常。

function initialize(Latitude, Longitude) { 
     var latlng = new google.maps.LatLng(Latitude, Longitude); 
     var map = new google.maps.Map(document.getElementById('map_'), { 
      center: latlng, 
      zoom: 13 
     }); 
     var marker = new google.maps.Marker({ 
      map: map, 
      position: latlng, 
      draggable: false, 
      //anchorPoint: new google.maps.Point(0, -29) 
     }); 
     map.setCenter(new google.maps.LatLng(Latitude, Longitude)); 

     var infowindow = new google.maps.InfoWindow(); 
     currLoc= $('#lastLocation').text(); 
     google.maps.event.addListener(marker, 'click', function() { 
      var iwContent = '<div id="iw_container">' + 
      '<div class="iw_title"><b>Last Location</b> : <span class="lastLoc">' + GetAddress(Latitude, Longitude) + '<span></div></div>';    
      infowindow.setContent(iwContent);    
      infowindow.open(map, marker); 
     }); 
    setTimeout(function() { 
      google.maps.event.trigger(map, "resize"); 
      map.setCenter(new google.maps.LatLng(Latitude, Longitude)); 
     }, 300); 
    }