Google map API doesn't display correctly jQuery slideToggle

前端 未结 2 1835
北海茫月
北海茫月 2021-01-16 15:50

I put Google Map inside a div, and made a slideToggle onto the div. the Google Map doesn\'t display correctly.

\

相关标签:
2条回答
  • 2021-01-16 16:20

    You could wait until the map is loaded by adding an event listener for idle. This event will fire once the map is fully loaded and ready.

    google.maps.event.addListenerOnce(map, 'idle', function() {
        $('.hide').hide();
    });
    

    Here's the JSFiddle.

    EDIT:

    For anyone with a similar problem, here is the updated JSFiddle with the map positioned offscreen so it remains hidden without using display:none.

    0 讨论(0)
  • 2021-01-16 16:28

    you need to wait until map finished render then you set hide a map because map div was toggle when div on hiding and that make map looking wrong location init function should look like this

    function initialize() {
      var latlng = new google.maps.LatLng(48.89376,2.33742); 
        var settings = {
        zoom: 15,
          center: latlng,
          disableDefaultUI: true,
            zoomControl: true,
          mapTypeId: google.maps.MapTypeId.ROADMAP
      };
    
      var map = new google.maps.Map(document.getElementById("map"), settings);
      var marker=new google.maps.Marker({
        position:latlng,
      });
    
      marker.setMap(map);
    
      // on map idle set div hide
      google.maps.event.addListenerOnce(map, 'idle', function() {
          $('.hide').hide();
      });
    }
    
    0 讨论(0)
提交回复
热议问题