angular-google-maps更新模型

时间:2016-04-12 18:06:12

标签: angularjs google-maps model angular-google-maps

如何更新模型以加载新的标记数据集?

<ui-gmap-google-map center="map.center" zoom="map.zoom" bounds="map.bounds" refresh="map.refresh">
<ui-gmap-markers
    models="dataset"
    coords="'self'"
    icon="'icon'"
    doCluster='10'
    click="onClick"
    >
        <ui-gmap-windows show="show">
            <div ng-non-bindable>
                <h3 class="gray3-text fz13 mbottom0">{{title}}</h3>
            </div>
        </ui-gmap-windows>
</ui-gmap-markers>

我正在更改ui-gmap-google-map指令外部的数据集,如此

<p ng-click="changeDataset('allUsers')" class="cursor-pointer">
<p ng-click="changeDataset('allQuotations')" class="cursor-pointer">

功能如下:

scope.changeDataset = function(){
    switch (dataset) {
         case 'allResellers':
             scope.dataset = scope.allResellers;
             break;
          case 'allQuotations':
              scope.dataset = scope.allQuotations;
              break;

           case 'allUsers':
              scope.dataset = scope.allUsers;
              break;

           case 'allMarkers':
              scope.dataset = scope.allMarkers;
              break;
       }
 };

我的scope.dataset发生了变化,但它没有更新地图中的标记。

PS:modelsbyref =&#34; false&#34;不起作用。

添加control =&#34; markerControl&#34;到ui-gmap-google-map工作,但必须使用此解决方法:

angular.element(document.getElementsByClassName('angular-google-map')).scope().markerControl.updateModels(newDataset);


<ui-gmap-google-map center="map.center" zoom="map.zoom" bounds="map.bounds">
<ui-gmap-markers
    models="dataset"
    coords="'self'"
    icon="'icon'"
    doCluster='10'
    click="onClick"
    control="markerControl"
    >
        <ui-gmap-windows show="show">
            <div ng-non-bindable>
                <h3 class="gray3-text fz13 mbottom0">{{title}}</h3>
            </div>
        </ui-gmap-windows>
</ui-gmap-markers>

2 个答案:

答案 0 :(得分:0)

由于changeDataset函数需要数据集名称,因此在进行以下更改之后:

$scope.changeDataset = function(dataset) {
        switch (dataset) {
            case 'allResellers':
                $scope.dataset = $scope.allResellers;
                break;
            case 'allQuotations':
                $scope.dataset = $scope.allQuotations;
                break;

            case 'allUsers':
                $scope.dataset = $scope.allUsers;
                break;

            case 'allMarkers':
                $scope.dataset = $scope.allMarkers;
                break;
        }
    };  

地图模型正在正确更新。

工作示例

angular.module('appMaps', ['uiGmapgoogle-maps'])
    .controller('mapCtrl', function($scope, uiGmapGoogleMapApi) {

        $scope.dataset = [
            { id: 1, title: "Brisbane", latitude: -33.867396, longitude: 151.206854 },
            { id: 2, title: "Sydney", latitude: -27.46758, longitude: 153.027892 },
            { id: 3, title: "Perth", latitude: -31.953159, longitude: 115.849915 }
        ];

        $scope.allResellers = [];
        $scope.allQuotations = [
            { id: 1, title: "Q1", latitude: -31.867396, longitude: 144.206854 },
            { id: 2, title: "Q2", latitude: -28.46758, longitude: 152.027892 }
        ];
        $scope.allUsers = [
            { id: 1, title: "User1", latitude: -36.867396, longitude: 145.206854 },
            { id: 2, title: "User2", latitude: -33.46758, longitude: 142.027892 },
        ];
        $scope.allMarkers = [];



        uiGmapGoogleMapApi.then(function(maps) {
            $scope.map = {
                center: { latitude: -30, longitude: 135.0 },
                zoom: 4,
                control: {},
                markerEvents: {
                    click: function(marker) {
                    }
                },
                options: $scope.mapOptions
            };
        });

        $scope.mapOptions = {};


        $scope.changeDataset = function(dataset) {
            switch (dataset) {
                case 'allResellers':
                    $scope.dataset = $scope.allResellers;
                    break;
                case 'allQuotations':
                    $scope.dataset = $scope.allQuotations;
                    break;

                case 'allUsers':
                    $scope.dataset = $scope.allUsers;
                    break;

                case 'allMarkers':
                    $scope.dataset = $scope.allMarkers;
                    break;
            }
        };




    });
.angular-google-map-container { height: 480px; }
<script src="https://code.angularjs.org/1.3.14/angular.js"></script>
<script src="http://rawgit.com/nmccready/angular-simple-logger/master/dist/angular-simple-logger.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-google-maps/2.2.1/angular-google-maps.min.js"></script>

<div ng-app="appMaps" ng-controller="mapCtrl">


    <p ng-click="changeDataset('allUsers')" class="cursor-pointer" >Display users</p>
    <p ng-click="changeDataset('allQuotations')" class="cursor-pointer" >Display quotations</p><br/>

    <ui-gmap-google-map center="map.center" zoom="map.zoom" bounds="map.bounds" refresh="map.refresh">
        <ui-gmap-markers models="dataset" coords="'self'" icon="'icon'" doCluster='10' click="onClick">
            <ui-gmap-windows show="show">
                <div ng-non-bindable>
                    <h3 class="gray3-text fz13 mbottom0">{{title}}</h3>
                </div>
            </ui-gmap-windows>
        </ui-gmap-markers>
</div>

答案 1 :(得分:0)

与此问题angular-google-maps updating marker model

中的问题相同

似乎每当我们更新标记时angularjs都不会立即更新它。在将新标记推送到数组后,只需调用 $ scope。$ digest()

同时检查 $ scope。$ apply()函数,它会自动调用$ scope。$ digest()。 请参阅本教程详细说明: tutorial showing $scope.$digest(), $scope.$apply, $scope.watch

它写在上面的教程中:

  

您可能会遇到AngularJS没有为您调用$ digest()函数的一些极端情况。您通常会通过注意数据绑定不会更新显示的值来检测到这一点。在这种情况下,调用$ scope。$ digest(),它应该工作。或者,您也许可以使用$ scope。$ apply()