Angular-显示特定ID内的谷歌地图

时间:2016-07-28 06:33:51

标签: javascript angularjs google-maps google-maps-markers angular-ui-bootstrap

我想使用 Angularjs 在ui-booststrap模式中的特定容器内显示Google地图。这是我的代码:

HTML:

<script type="text/ng-template" id="mapModalContent.html">
    <div class="map-container" ng-class="{'open' : openMapContainer}">
        <div class="header-section">
        header
        </div>
        <div class="close-btn"><i class="icon icon-cancel"></i></div>
        <div id="map-test" class="map-test"></div>
        <div id="map-content"></div>
        <div class="footer-section"></div>
    </div>
</script>

角:

    var modalInstance = $uibModal.open({
        templateUrl: 'mapModalContent.html',
        scope: $scope,
        windowClass: 'mapModal',
        size: 'lg',
        resolve: {
            data: function () {
                return $scope;
            }
        }
    });
        modalInstance.opened.then(function() {
            console.log('opened');
        });
    }
    $scope.$watch('data', function(value){
        if(typeof value !== 'undefined') {
            searchLatLng = new google.maps.LatLng($scope.data.latitude, $scope.data.longitude);

             googleMap = new google.maps.Map($element.find('.map-content'), {
                 center: {lat: -34.397, lng: 150.644},
                 zoom: 8
             });
            searchMarker = new google.maps.Marker({
                position: searchLatLng,
                map: googleMap,
                draggable: true
            });
            google.maps.event.addListener(searchMarker, 'dragend', function () {
                scope.$apply(function () {
                    $scope.data.latitude = searchMarker.getPosition().lat();
                    $scope.data.longitude = searchMarker.getPosition().lng();
                });
            }.bind(this));

            var myPosition = new google.maps.LatLng($scope.data.latitude, $scope.data.longitude);
            searchMarker.setPosition(myPosition);
        }
    }, true);
}       

在控制台中,我得到了

打开(用于打开模态)

enter image description here

实际上,地图还没有加载。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

如果我理解正确,有一个灰色的框。

你可以试试这个:

modalInstance.opened.then(function() {
    console.log('opened');
     google.maps.event.trigger(googleMap, "resize");
});

Grey boxes appear in parts of embedded Google Map in modal box