angular-google-maps does now show correctly when it's hidden at start

天大地大妈咪最大 提交于 2019-12-04 09:11:11

Try using ng-if instead ng-show/ng-hide. Angular-google-maps has an issue with ng-show /ng-hide.

See this https://github.com/nlaplante/angular-google-maps/issues/291

Kristian Benoit

I found the solution here

Add a control attribute to the google-map tag:

<google-map
    center="mapOption.center"
    zoom="mapOption.zoom"
    control="myGoogleMap">
</google-map>

Then in the controller, set $scope.myGoogleMap to {}, it will be filled when the maps get initialized. After that you can use $scope.myGoogleMap.refresh() to send a resize to the map !

Here's the controller working.

app.controller("myCtrl", function($scope, $timeout) {
    $scope.mapOption = {
         center: {
             latitude: 45.4,
             longitude: -71.9
         },
         zoom: 11
    };
    $scope.visible = false;
    $scope.mapViewPosition = {};
    $scope.$watch("visible", function(newvalue) {
        $timeout(function() {
             var map = $scope.myGoogleMap.refresh();
        }, 0);
    });
});

You need to setup map element css before showing

                        const partMap = document.getElementById(mapDivId);


                            partMap.style.height = "400px";
                            partMap.style.width = "800px";
                            partMap.style.marginLeft = 'auto';
                            partMap.style.marginRight = 'auto';
                            const center = new google.maps.LatLng(center_lat, center_lon);
                            const mapOptions = {
                                zoom: 8,
                                scrollwheel: false,
                                center: center,
                                mapTypeId: google.maps.MapTypeId.ROADMAP
                            };

Google map does not work when element does not have explicit styling and somehow styling information is not available when element is in "ng-hide" mode.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!