angular-google-maps icon indicating copy to clipboard operation
angular-google-maps copied to clipboard

When I change the page and I'm back the map is broken

Open giulianok opened this issue 11 years ago • 17 comments

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?

screen shot 2015-01-25 at 10 41 05 pm screen shot 2015-01-25 at 10 41 16 pm

giulianok avatar Jan 26 '15 03:01 giulianok

@zacronos how did we work around this again?

nmccready avatar Jan 26 '15 12:01 nmccready

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.

nmccready avatar Jan 26 '15 12:01 nmccready

@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.

zacronos avatar Jan 26 '15 13:01 zacronos

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 avatar Jan 26 '15 14:01 nmccready

@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.

zacronos avatar Jan 26 '15 14:01 zacronos

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 avatar Jan 26 '15 16:01 nmccready

@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.

zacronos avatar Jan 26 '15 17:01 zacronos

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.

zacronos avatar Jan 26 '15 17:01 zacronos

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:

map....

THANKS

giulianok avatar Jan 27 '15 02:01 giulianok

@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 avatar Jan 27 '15 03:01 zacronos

@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

nmccready avatar Feb 04 '15 18:02 nmccready

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.

nmccready avatar Feb 04 '15 21:02 nmccready

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.

aosi87 avatar Feb 17 '16 18:02 aosi87

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.

chris-eaton avatar Apr 01 '16 09:04 chris-eaton

@nmccready Thank You. It worked Great.

jawadahmadd avatar Aug 09 '16 09:08 jawadahmadd

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

Seb- avatar Aug 09 '16 18:08 Seb-

    function refreshGoogleMap() {
        $timeout(function () {
            vm.isGoogleMapReady = false;
            $timeout(function () {
                vm.isGoogleMapReady = true;
            });
        });
    }

ccaymaz avatar Nov 05 '16 00:11 ccaymaz