angular-google-maps updating marker model

随声附和 提交于 2019-12-06 12:51:13

问题


Im using the angular-google-maps library but I cannot seem to get the map to update when I dynamically add new markers to my model. Hardcoded markers in the model are correctly displayed.

HTML

<ui-gmap-markers models="map.placeMarkers" coords="'self'" icon="'icon'" options="'options'" click="'onClicked'" modelsbyref="true"></ui-gmap-markers>

JS

$scope.map.placeMarkers = [
            {
                id: 1,
                latitude: 45,
                longitude: -74,
                options: {},
                title: 'Test 123'
            },
            {
                id: 2,
                latitude: 15,
                longitude: 30,
                options: {},
                title: 'Test 1234'
            }
        ];

The above is working fine however when I do:

var place = {
   id: 3,
   latitude: 455,
   longitude: -574,
   options: {},
   title: 'Test 1233455'
};

$scope.map.placeMarkers.push(place);

The model is update but not the map. I have tried using $scope.$apply() but got an error since a digest cycle was already in progress.


回答1:


you need to set modelsbyref to false in ui-gmap-markers directives

 <ui-gmap-markers models="places" coords="'self'" modelsbyref="false">  

   </ui-gmap-markers>



回答2:


Same happened with me. Spent hours to find solutions.

It seemed like whenever we update markers angularjs doesn't update it immediately. just call $scope.$digest() after pushing new marker to the array.

Also check $scope.$apply() function, it calls $scope.$digest() automatically. Please refer to this tutorial which explain in detail: tutorial showing $scope.$digest(), $scope.$apply, $scope.watch

Its written in above tutorial:

You may encounter some corner cases where AngularJS does not call the $digest() function for you. You will usually detect that by noticing that the data bindings do not upate the displayed values. In that case, call $scope.$digest() and it should work. Or, you can perhaps use $scope.$apply()



来源:https://stackoverflow.com/questions/28344687/angular-google-maps-updating-marker-model

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