When I change the page and I'm back the map is broken
Hi, I have this issue. My map is on the background, like this:
.angular-google-map{ position: absolute; &, .angular-google-map-container{ top: 0; left: 0; width: 100%; height: 100%; } } (this is sass)
The first time the map is rendered correctly, but, when I change the page and I'm back, the map is broken.
Here the screenshots, how can I fix it?

@zacronos how did we work around this again?
Try putting an ng-if around the map, and when you change tabs disable the if (set to false). Then back to true. See if that refreshes things.
@giulianok, @nmccready, yes I've looked at this problem too -- I'm not sure exactly why, but if I recall correctly I think the issue comes in when the map is initialized a 2nd time. So our workaround was to ensure the map never gets destroyed, so it never has to be reinitialized.
Try looking at ui-router-extras for one way to do this (it's what we used).
If you ensure the state containing the map (and any of its ancestor states) are sticky, then that state (and its views) will never be destroyed, simply hidden and restored. The intended use case for this is a tab system, so if that is what you are doing this is probably something you'll want anyway.
This example turns the map on and off with no issues as well.
http://rawgit.com/angular-ui/angular-google-maps/master/example/two-maps.html
https://github.com/angular-ui/angular-google-maps/blob/master/example/two-maps.html
@nmccready, on that example, when I hit RefreshMap2, the 2nd map goes blank until I interact with the 1st map again. That could be the same issue, but since both instances are conceptually the same map (1st init and 2nd init), there's no way to interact with the first and thus make the 2nd come back.
It comes back on for me by hitting refresh 1 button. That is probably not right, but it does possibly hint to the problem.
@nmccready, you're right, hitting the Refresh Map button also fixes map2. That is interesting.
I wonder if the problem in this example is simply using _.defer() instead of $scope.$evalAsync() here: https://github.com/angular-ui/angular-google-maps/blob/master/example/assets/scripts/controllers/two-maps.js#L86 If so, I don't think this is the same issue, just a different (minor) bug in this example.
Looking at that code also raises the question though of why map2.refresh() sets showMap to false and then true, while map.refresh() doesn't -- really, map2 shouldn't take any extra work that map doesn't, and if it does that also might point to an underlying bug.
I'll see if I can make an example closer to what we're discussing here and put it in PR.
I've confirmed the bug in two-maps is just a result of using _.defer(), but I can't seem to make a simple example that shows this problem. I can confirm from experience that it is real, however.
A good example might require setting up ui-router, since in both cases it sounds like the issue wasn't merely using ng-if or ng-show, but changing states within a single-page app. There could be something funky happening only when mixed with ui-router, though I have no idea what it could be.
Thank you guys. Sorry for the hour, I have time at the night to test the map.
@nmccready saids "Try putting an ng-if around the map, and when you change tabs disable the if (set to false). Then back to true. See if that refreshes things."
So, I tried and I worked.
Here how I did, I think it's not the best, but it's work. If somebody has an another solution please tell me
uiGmapGoogleMapApi.then(function (maps) {
setTimeout(function () {
$scope.showMap = true;
$scope.$apply();
}, 100);
});
and, in the html:
THANKS
@giulianok, if you ever find yourself using $scope.$apply(), there's probably a better way. Give this a try instead:
uiGmapGoogleMapApi.then(function (maps) {
$scope.$evalAsync(function () {
$scope.showMap = true;
});
});
If that doesn't work, then a slightly simpler version of your code would be:
uiGmapGoogleMapApi.then(function (maps) {
$timeout(function () {
$scope.showMap = true;
}, 100);
});
@zacronos removed _.defer , there is an ng-router example which does not have the map draw issue at all. https://github.com/angular-ui/angular-google-maps/blob/master/example/issue-755-ng-router.html
I think I have found the answer here. http://support.advancedcustomfields.com/forums/topic/google-map-size-and-not-centered/
It is a google maps problem. Look for Elliot Condon's answer. We need to call
google.maps.event.trigger(this.map, 'resize'); at map initialization.. maybe even delay it. Also we should call this in refresh as well.
Well i know this is closed but i have a comment about this issue, this occurs every time ui-view changes states, ui-view works a bit tricky, it adds HTML before it removes the old one thats why some times the map show a different center or the marks does not appear in the view, now this will not occur if you manage your CSS to have static map height and width, but on dynamic or responsive CSS this may be a problem. I found this when i was implementing "resolve" feature, my map takes the both HTML templates height so when ui-view transition ends my map is broken, found this after a few hours of debug, i couldn't avoid this behavior, so i just add more CSS and use display tricks on $stateChangeStart to solve this.
Cheers.
Here is the code I use to work around this issue.
mapApi.callWhenViewTransitionComplete = function (scope, functionToCall) {
// Event is fired when UI router thinks the view content is loaded
var unregister = $rootScope.$on('$viewContentLoaded', function (event) {
// The browser, at this point, will not have recalculated the dimensions of dynamically sized objects
// This timeout simply delays execution of the callback until after the browser has re-calculated
$timeout(function () {
if (functionToCall) {
functionToCall();
}
// We only want to receive this event once per controller
unregister();
});
});
}
This code is in my common MapService and is called, from within a controller, like so
mapService.callWhenViewTransitionComplete($scope, $scope.init);
My init function then sets up the maps as I need them.
@nmccready Thank You. It worked Great.
Here is the code I used to fix this issue.
uiGmapIsReady.promise(1).then(function(instances) {
instances.forEach(function(inst) {
console.log("uiGmapIsReady");
refreshMap($scope.map.center); // custom function calling $scope.map.control.refresh();
});
});
https://angular-ui.github.io/angular-google-maps/#!/api/IsReady
function refreshGoogleMap() {
$timeout(function () {
vm.isGoogleMapReady = false;
$timeout(function () {
vm.isGoogleMapReady = true;
});
});
}