Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are not all Google Map Tiles rendered on the initial load of an AngularJS app?

Working on a Google Map app built with AngularJS.

Currently deployed sandbox: http://of.xchg.com/

Click Map link and you will notice only ONE tile IF ANY are rendered.

If you then RESIZE the browser window, THEN the rest of the map tiles will render completely.

Here is the git project: https://github.com/LarryEitel/of4

Why are not all Google Map Tiles rendered?

Is there a way to test for complete rendering of tiles?

Is there a way to force re-rendering of all map tiles?

like image 871
Larry Eitel Avatar asked May 02 '13 21:05

Larry Eitel


People also ask

What are tiles in Google Map?

Tiles in Google Maps are numbered from the same origin as that for pixels. For Google's implementation of the Mercator projection, the origin tile is always at the northwest corner of the map, with x values increasing from west to east and y values increasing from north to south.


2 Answers

Call this function in your map controller:

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

That should do the trick.

like image 91
jesal Avatar answered Sep 23 '22 22:09

jesal


I found this problem when trying to re-center the map after the view loaded. There would be a single tile in the top left corner.

I noticed that it was centering the map at the topleft of the div (0,0), and figured that it must be measuring the div before it was visible. This would mean the dimensions were 0px, 0px and so to center the map would mean placing a single tile at (0,0).

I tried the solution of triggering the resize event

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

and that almost worked - it filled the div with tiles, but it was still centered at (0,0).

What worked was to wrap the re-center code in a $timeout(). This ensures that the div is fully loaded and correctly sized before the code runs. Now it works perfectly.

$timeout(function() {
    // still need this.
    google.maps.event.trigger(map, 'resize');   
    map.setCenter(53, -6);
});
like image 30
Owen O Byrne Avatar answered Sep 23 '22 22:09

Owen O Byrne