Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

show loading image while loading google map

I'm using Google Maps Javascript API for showing a map in my site with aditional markers. This is the trigger

var map = new google.maps.Map(document.getElementById("map_canvas"), settings);

This works fine, but the map takes a few seconds to show. I'm putting a loading image in the map div like this:

<div id="map_canvas" style="width:700px; height:500px"><img src="/image/ajax-loader.gif" /></div>

But the image never shows, just the blank page until the map shows.

The image is working, because if I disable the map loading function, the image is there. So I think the google map clears the div before the map is loaded.

Any ideas how to show a loading feedback to the user while waiting? I could not find a function in the API...

like image 339
Alex Angelico Avatar asked Feb 16 '11 19:02

Alex Angelico


People also ask

How do I know when a Google map is loaded?

If you are using if (google. maps) {...} and it gives you a reference error if google is undefined (i.e. if the API didn't load). Then you can use this, if (typeof google === 'object' && typeof google. maps === 'object') {...} to check if it loaded successfully.

How do I get Google Maps to load?

If it's turned on, scroll down, tap "Google Maps," and select either "While Using the App" or "Always." If none of the above works, you may want to try uninstalling and reinstalling the app on your phone. And if you're still having issues, you should try restarting your iPhone or Android phone.


1 Answers

It's easier to add a GIF via CSS to the background of the Map DIV.

i.e.

html

<div id="map_canvas"></div>

css

#map_canvas {background: transparent url(images/ajax-loading.gif) no-repeat center center;}

the loading GIF will stay visible until the map is loaded. Very handy if your using the sensor to get the current location, as this takes a while. As soon as google maps is ready to populate #map_canvas with it's own images, you won't be able to see the loading GIF any more.

like image 181
Andrew Killen Avatar answered Nov 03 '22 04:11

Andrew Killen