Keep a Google Maps v3 Map hidden, show when needed

2020-01-30 07:24发布

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 by using an argument to the new google.maps.Map constructor, or a MapOption ?

Any thoughts on this?

Thank you in advance!

8条回答
在下西门庆
2楼-- · 2020-01-30 07:51

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

$(document).ready(function(){
    $('#Checkbox').click(function(){
        $('#googleMapDiv').toggle();
        initialize(); // initialize the map
    });
});
查看更多
时光不老,我们不散
3楼-- · 2020-01-30 07:55

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

查看更多
够拽才男人
4楼-- · 2020-01-30 07:58

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.

查看更多
乱世女痞
5楼-- · 2020-01-30 08:02

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 });
}); 
查看更多
劳资没心,怎么记你
6楼-- · 2020-01-30 08:03

This will work

    google.maps.event.addListener(map, "idle", function ()
    {
        google.maps.event.trigger(map, 'resize');
    });
查看更多
Rolldiameter
7楼-- · 2020-01-30 08:04

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.

查看更多
登录 后发表回答