动态添加第二个Google Map后无法正确呈现

JMK

将以下HTML复制到.html文件并在浏览器中查看时,直接显示了我的问题。

<!DOCTYPE HTML>
<html ng-app="myApp">

<head>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.16/angular.min.js"> </script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.10.0/ui-bootstrap-tpls.min.js"> </script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=places&sensor=false"> </script>

<script>

    var mapOptions = {
        zoom: 5,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
    };

    var myApp = angular.module('myApp', ['ui.bootstrap']);

    var myController = function($scope){
        $scope.locations = [];

        $scope.addLocation = function(){
            var id = $scope.locations.length+1;

            $scope.locations.push({ id: id, city: $scope.newLocation.city, country: $scope.newLocation.country });

            $scope.newLocation = { city:'', country:'' };
        }

        $scope.$watch('locations', function(){

            for(var i = 0; i < $scope.locations.length; i++){
                var thisLocation = $scope.locations[i];

                if(!thisLocation.rendered){
                    var geocoder = new google.maps.Geocoder();

                    var id = thisLocation.id;
                    var address = thisLocation.city + ' ' + thisLocation.country;

                    geocoder.geocode({ 'address': address }, function (results, status) {
                        if (status == google.maps.GeocoderStatus.OK) {
                            var longitude = results[0].geometry.location.A;
                            var latitude = results[0].geometry.location.k;

                            var map = $scope.displayMap(latitude, longitude, 'Test' + id, id);
                            google.maps.event.trigger(map, "resize");
                        }
                    });

                    thisLocation.rendered = true;
                }


            }

        }, true);

        $scope.displayMap = function displayMap(latitude, longitude, name, id) {

            var mapDiv = document.getElementById('map' + id);
            if (mapDiv) mapDiv.style.display = "block";

            var mapCanvas = document.getElementById('map-canvas' + id);
            if (mapCanvas) mapCanvas.style.height = "400px";

            var map = new google.maps.Map(document.getElementById('map-canvas' + id), mapOptions);

            var locCoords = new google.maps.LatLng(latitude, longitude);

            map.setCenter(locCoords);

            new google.maps.Marker({
                position: locCoords,
                map: map,
                title: name
            });

            return map;
        }

        $scope.newLocation = { city:'', country:'' };
    }

    myController.$inject = ['$scope'];
    myApp.controller('myController', myController);
</script>

</head>

<body ng-controller="myController">

<form ng-submit="addLocation()">
    <label for="city"><input id="city" ng-model="newLocation.city" required /></label>
    <label for="country"><input id="country" ng-model="newLocation.country" required /></label>
    <input type="submit" value="Submit" />
</form>

<tabset>
    <tab ng-repeat="location in locations" heading="{{location.city}}">
        <div id="map{{location.id}}" class="map">
            <div id="map-canvas{{location.id}}" class="map-canvas">
                Map goes here
            </div>
        </div>
    </tab>
</tabset>

</body>

</html>

基本上,您只需填写一次表单即可将地图添加到选项卡集,并且效果很好:

第一张地图看起来不错

再次填写表单以添加第二张地图,这很麻烦:

第二地图,灾区

Chrome的开发者控制台中的任何内容均未表明存在问题。

我不知道这是Google Maps问题,Angular问题还是Bootstrap问题的Angular指令,所以我使用这三个模型创建了这个模型,并尽可能地模仿了我的实际代码。

任何人都可以对这里发生的事情有所了解吗?

谢谢

JMK

好的,事实证明Google Maps无法处理非活动选项卡上的呈现,因此我需要创建选项卡并将其设置为活动状态,然后再呈现地图。

我变了:

$scope.locations.push({ id: id, city: $scope.newLocation.city, country: $scope.newLocation.country });

至:

$scope.locations.push({ id: id, city: $scope.newLocation.city, country: $scope.newLocation.country, active: true });

在我的for循环的末尾添加了以下内容:

thisLocation.active = true;

将Angular设置为true时,似乎足以将其他选项卡的active属性设置为false。

最后,我将tab元素的active属性设置为模型的active属性:

<tab ng-repeat="location in locations" heading="{{location.city}}" active="location.active" >

下面是一个工作示例:

<!DOCTYPE HTML>
<html ng-app="myApp">

<head>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.16/angular.min.js"> </script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.10.0/ui-bootstrap-tpls.min.js"> </script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=places&sensor=false"> </script>

<script>

    var mapOptions = {
        zoom: 5,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
    };

    var myApp = angular.module('myApp', ['ui.bootstrap']);

    var myController = function($scope){
        $scope.locations = [];

        $scope.addLocation = function(){
            var id = $scope.locations.length+1;

            $scope.locations.push({ id: id, city: $scope.newLocation.city, country: $scope.newLocation.country, active: true });

            $scope.newLocation = { city:'', country:'' };
        }

        $scope.$watch('locations', function(){

            for(var i = 0; i < $scope.locations.length; i++){
                var thisLocation = $scope.locations[i];

                if(!thisLocation.rendered){ 

                    var geocoder = new google.maps.Geocoder();

                    var id = thisLocation.id;
                    var address = thisLocation.city + ' ' + thisLocation.country;

                    geocoder.geocode({ 'address': address }, function (results, status) {
                        if (status == google.maps.GeocoderStatus.OK) {
                            var longitude = results[0].geometry.location.A;
                            var latitude = results[0].geometry.location.k;

                            var map = $scope.displayMap(latitude, longitude, 'Test' + id, id);
                            google.maps.event.trigger(map, "resize");
                        }
                    });

                    thisLocation.active = true;
                    thisLocation.rendered = true;
                }


            }

        }, true);

        $scope.displayMap = function displayMap(latitude, longitude, name, id) {

            var mapDiv = document.getElementById('map' + id);
            if (mapDiv) mapDiv.style.display = "block";

            var mapCanvas = document.getElementById('map-canvas' + id);
            if (mapCanvas) mapCanvas.style.height = "400px";

            var map = new google.maps.Map(document.getElementById('map-canvas' + id), mapOptions);

            var locCoords = new google.maps.LatLng(latitude, longitude);

            map.setCenter(locCoords);

            new google.maps.Marker({
                position: locCoords,
                map: map,
                title: name
            });

            google.maps.event.trigger(map, "resize");

            return map;
        }

        $scope.newLocation = { city:'', country:'' };
    }

    myController.$inject = ['$scope'];
    myApp.controller('myController', myController);
</script>

</head>

<body ng-controller="myController">

<form ng-submit="addLocation()">
    <label for="city"><input id="city" ng-model="newLocation.city" required /></label>
    <label for="country"><input id="country" ng-model="newLocation.country" required /></label>
    <input type="submit" value="Submit" />
</form>

<tabset>
    <tab ng-repeat="location in locations" heading="{{location.city}}" active="location.active" >
        <div id="map{{location.id}}" class="map">
            <div id="map-canvas{{location.id}}" class="map-canvas">
                Map goes here
            </div>
        </div>
    </tab>
</tabset>

</body>

</html>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从NavigationController呈现TabBarController的第二个Tab

来自分类Dev

无法在我的表单中添加第二个下拉列表

来自分类Dev

无法在第二个图像后添加jQuery的东西

来自分类Dev

排序后无法保留第二个属性

来自分类Dev

WP动态特色图片-无法获取第二个特色图片网址

来自分类Dev

使用JavaScript在每第二个字符后添加冒号(:)

来自分类Dev

在第二个空格后删除字符

来自分类Dev

添加第二个节点后,Cassandra中的常量超时

来自分类Dev

添加第二个驱动器后,平均负载较高

来自分类Dev

在每个逗号后查找第二个空格

来自分类Dev

JavaScript:无法将第二个值添加到单链表

来自分类Dev

添加第二个div后,第一个孩子倒下了

来自分类Dev

无法向Spring Data Starter项目添加第二个实体

来自分类Dev

动态添加第二个Google Map后无法正确呈现

来自分类Dev

在JavaFX中添加第二个事件处理程序后,拖动行为很奇怪

来自分类Dev

添加第二个复合组件后,错误<f:ajax>包含未知ID

来自分类Dev

添加第二个菜单时,我无法单击菜单上的元素

来自分类Dev

将第二个监视器添加到Windows 10后,Google Chrome浏览器将打开为黑色矩形

来自分类Dev

无法在回收站视图中添加第二个孩子

来自分类Dev

添加第二个数据表控件后发生错误

来自分类Dev

在前缀后添加第二个大写字母

来自分类Dev

无法添加第二个ng-controller

来自分类Dev

第二个 svg 语句不会呈现

来自分类Dev

添加第二个驱动器后平均负载很高

来自分类Dev

OpenGL - 添加第二个网格后第一个网格消失

来自分类Dev

添加第二个模态框后模态框停止正常工作

来自分类Dev

Javascript:在字符串的第二个字符后添加整数

来自分类Dev

无法从第二个数据源动态添加自定义单元格

来自分类Dev

无法循环并为数组添加第二个值

Related 相关文章

  1. 1

    从NavigationController呈现TabBarController的第二个Tab

  2. 2

    无法在我的表单中添加第二个下拉列表

  3. 3

    无法在第二个图像后添加jQuery的东西

  4. 4

    排序后无法保留第二个属性

  5. 5

    WP动态特色图片-无法获取第二个特色图片网址

  6. 6

    使用JavaScript在每第二个字符后添加冒号(:)

  7. 7

    在第二个空格后删除字符

  8. 8

    添加第二个节点后,Cassandra中的常量超时

  9. 9

    添加第二个驱动器后,平均负载较高

  10. 10

    在每个逗号后查找第二个空格

  11. 11

    JavaScript:无法将第二个值添加到单链表

  12. 12

    添加第二个div后,第一个孩子倒下了

  13. 13

    无法向Spring Data Starter项目添加第二个实体

  14. 14

    动态添加第二个Google Map后无法正确呈现

  15. 15

    在JavaFX中添加第二个事件处理程序后,拖动行为很奇怪

  16. 16

    添加第二个复合组件后,错误<f:ajax>包含未知ID

  17. 17

    添加第二个菜单时,我无法单击菜单上的元素

  18. 18

    将第二个监视器添加到Windows 10后,Google Chrome浏览器将打开为黑色矩形

  19. 19

    无法在回收站视图中添加第二个孩子

  20. 20

    添加第二个数据表控件后发生错误

  21. 21

    在前缀后添加第二个大写字母

  22. 22

    无法添加第二个ng-controller

  23. 23

    第二个 svg 语句不会呈现

  24. 24

    添加第二个驱动器后平均负载很高

  25. 25

    OpenGL - 添加第二个网格后第一个网格消失

  26. 26

    添加第二个模态框后模态框停止正常工作

  27. 27

    Javascript:在字符串的第二个字符后添加整数

  28. 28

    无法从第二个数据源动态添加自定义单元格

  29. 29

    无法循环并为数组添加第二个值

热门标签

归档