最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to trigger map resize event after the angular.js ui-map directive is rendered? - Stack Overflow

programmeradmin5浏览0评论

The ui-map example works perfectly when using on a simple angular.js page. Now I'm using the ui-router and trying to display the map inside a view. What happens is that the map tiles are only displayed on half of the map. If I resize the window, the map is displayed correctly.

I think I have to trigger the Google Maps resize event once the view is rendered, but how?

google.maps.event.trigger(<myMapObject>, 'resize'); 

The ui-map example works perfectly when using on a simple angular.js page. Now I'm using the ui-router and trying to display the map inside a view. What happens is that the map tiles are only displayed on half of the map. If I resize the window, the map is displayed correctly.

I think I have to trigger the Google Maps resize event once the view is rendered, but how?

google.maps.event.trigger(<myMapObject>, 'resize'); 

Share Improve this question asked Sep 12, 2013 at 16:08 Federico EllesFederico Elles 4,6797 gold badges28 silver badges35 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Wait for a while before calling trigger, I had this same issue and found resolved here:

AngularJS: map via Google Maps API v3 in a tab

window.setTimeout(function(){
    google.maps.event.trigger(map, 'resize');
},100);

that work with angular 1.3.15

$timeout(function(){
         angular.forEach($scope.maps, function(index) {
            google.maps.event.trigger(index, 'resize');
          });
      }, 100);
    }
    $scope.maps = [];
    $scope.$on('mapInitialized', function(evt, evtMap) {
          $scope.maps.push(evtMap);
    });

http://plnkr.co/edit/3P4iLTrog1ismFDUQNQe?p=preview


other solutions


without adding $timeout you could also use $scope.$applyAsync() that runs code within the same digest cycle

发布评论

评论列表(0)

  1. 暂无评论