Keep a Google Maps v3 Map hidden, show when needed

后端 未结 8 966
北海茫月
北海茫月 2020-12-05 04:34

Is there a way of preventing a Google Maps (JS, v3) map being displayed from the get-go? I\'m doing some pre-processing and would like to show my \'Loading\' spinner until e

相关标签:
8条回答
  • 2020-12-05 05:06

    depending on what you are doing another posibility could be to have multiple bools you set to true when each process is done.

    For example:

    if you have a geocode service running which you want to wait for, you could have a var called GeoState

    and in the result part of the geocoder set GeoState to true, then have a timed function check if all the services have returned true, when they have, make the map visible.

    0 讨论(0)
  • 2020-12-05 05:08

    this works fine for me, I use jquery tabs

    setTimeout(function() {
            google.maps.event.trigger(map, "resize");
            map.setCenter(new google.maps.LatLng(default_lat, default_lng));
            map.setZoom(default_map_zoom);
        }, 2000);
    

    om this link https://code.google.com/p/gmaps-api-issues/issues/detail?id=1448

    0 讨论(0)
  • 2020-12-05 05:12

    better way:

    gmap.redraw = function() {
        gmOnLoad = true;
        if(gmOnLoad) {
            google.maps.event.trigger(gmap, "resize");
            gmap.setCenter(gmlatlng);
            gmOnLoad = false;
        }
    }
    

    and in show click event:

    $("#goo").click(function() {
      if ($("#map_canvas").css("display") == "none") {
        $("#YMapsID").toggle();
        $("#map_canvas").toggle();
        if (gmap != undefined) {
            gmap.redraw();
        }
      }
    });
    
    0 讨论(0)
  • 2020-12-05 05:13

    This works for me. I'm using the JQuery library.

    $(document).ready(function(){
        $('#Checkbox').click(function(){
            $('#googleMapDiv').toggle();
            initialize(); // initialize the map
        });
    });
    
    0 讨论(0)
  • 2020-12-05 05:16

    Also remember to call:

    google.maps.event.trigger(map, 'resize');
    

    if you have changed the size of the <div>. A display:none <div> has no size.

    0 讨论(0)
  • 2020-12-05 05:18

    Or you could just hide it like with css visablility or css opacity.

    $("#GoogleMap").css({ opacity: 0, zoom: 0 });
    initialize();
    
    google.maps.event.addListener(map,"idle", function(){ 
         $('#Loader').hide();
         $("#GoogleMap").css({ opacity: 1, zoom: 1 });
    }); 
    
    0 讨论(0)
提交回复
热议问题