开发者

Keep a Google Maps v3 Map hidden, show when needed

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 everything is good to go (more eloquently put, hide the map -- e.g. the container div – until all pre-processing is complete – at which point, show the map).

Hooking up the map's idle event doesn't help that much, since the map is already displayed when this event hits.

I know that the container div gets inline-styled by GMaps after loading, my first idea was to clear out the style attribute (whilst listening to the idle event), but it would be interesting to see if there is a way of creating the map and not displaying it until all pre-processing is done.

Maybe b开发者_StackOverflowy using an argument to the new google.maps.Map constructor, or a MapOption ?

Any thoughts on this?

Thank you in advance!


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.


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 });
}); 


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

$(document).ready(function(){
    $('#Checkbox').click(function(){
        $('#googleMapDiv').toggle();
        initialize(); // initialize the map
    });
});


another way to show the hidden map when map is first time rendering the <div> is to set style: visibility.

When firstly hidden, use visibility = hidden; to show use visibility = visible

the reason is: visibility:hidden means that the contents of the element will be invisible, but the element stays in its original position and size.


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


This will work

    google.maps.event.addListener(map, "idle", function ()
    {
        google.maps.event.trigger(map, 'resize');
    });


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();
    }
  }
});


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜