how to deal with google map inside of a hidden div

2019-01-03 12:34发布

i have a page and a google map is inside a hidden div at first. I then show the div after i click a link but only the top left of the map shows up.

i tried having this code run after the click:

    map0.onResize();

or:

  google.maps.event.trigger(map0, 'resize')

any ideas. here is an image of what i see after showing the div with the hidden map in it.alt text

23条回答
戒情不戒烟
2楼-- · 2019-01-03 13:08

use this line of codes when you want to show map.

               $("#map_view").show("slow"); // use id of div which you want to show.
                    var script = document.createElement("script");
                    script.type = "text/javascript";
                    script.src = "https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&callback=initialize";
                    document.body.appendChild(script);
查看更多
何必那么认真
3楼-- · 2019-01-03 13:10

Add this code before the div or pass it to a js file:

<script>
    $(document).on("pageshow","#div_name",function(){
       initialize();
    });

   function initialize() {
   // create the map

      var myOptions = {
         zoom: 14,
         center: new google.maps.LatLng(0.0, 0.0),
         mapTypeId: google.maps.MapTypeId.ROADMAP
      }
      map = new google.maps.Map(document.getElementById("div_name"), myOptions);
   }


</script>

This event will be triggered after the div loads so it will refresh the map content without having to press F5

查看更多
Juvenile、少年°
4楼-- · 2019-01-03 13:11

Upon showing the map I am geocoding a address then setting the maps center. The google.maps.event.trigger(map, 'resize'); did not work for me.

I had to use a map.setZoom(14);

Code below:

document.getElementById('map').style.display = 'block';
var geocoder = new google.maps.Geocoder();
        geocoder.geocode({ 'address': input.value }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
                var marker2 = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location
                });
                map.setZoom(14);
                marker2.addListener('dragend', function (event) {
                    $('#lat').val(event.latLng.lat());
                    $('#lng').val(event.latLng.lng());
                });

            }
        });
查看更多
可以哭但决不认输i
5楼-- · 2019-01-03 13:12

I've found this to work for me:

to hide:

$('.mapWrapper')
.css({
  visibility: 'hidden',
  height: 0
});

to show:

    $('.mapWrapper').css({
      visibility: 'visible',
      height: 'auto'
    });
查看更多
相关推荐>>
6楼-- · 2019-01-03 13:13

If used inside Bootstrap v3 tabs, the following should work:

$('a[href="#tab-location"]').on('shown.bs.tab', function(e){
    var center = map.getCenter();
    google.maps.event.trigger(map, 'resize');
    map.setCenter(center);
});

where tab-location is the ID of tab containing map.

查看更多
登录 后发表回答