【问题标题】:angular-google-maps updating marker modelangular-google-maps 更新标记模型
【发布时间】:2016-06-29 15:20:18
【问题描述】:

我正在使用 angular-google-maps 库,但当我向模型动态添加新标记时,我似乎无法更新地图。模型中的硬编码标记正确显示。

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'
            }
        ];

上面的工作正常但是当我这样做时:

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

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

模型已更新,但地图未更新。我曾尝试使用 $scope.$apply(),但由于摘要循环已在进行中,因此出现错误。

【问题讨论】:

  • 试试下面的链接。这是一个使用 Angular js 动态添加标记的工作示例。按照这个例子,希望它对你有用:) LINK
  • 我有同样的问题,你有解决方案吗? Ben_hawk
  • 是的,我已经很久没有使用这个库了,但是如果我没记错的话,性能已经发生了变化。您需要更改数组引用才能刷新地图。尝试在您的位置数组上使用角度副本并在地图位置属性上设置结果。让我知道它是否有效。

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


【解决方案1】:

您需要在 ui-gmap-markers 指令中将 modelsbyref 设置为 false

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

   </ui-gmap-markers>

【讨论】:

    【解决方案2】:

    同样的事情发生在我身上。花了几个小时寻找解决方案。

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

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

    上面的教程写的:

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

    【讨论】:

      猜你喜欢
      • 2013-12-28
      • 2021-09-22
      • 2011-09-05
      • 2012-03-26
      • 2012-01-28
      • 1970-01-01
      • 2015-11-04
      • 2017-06-28
      相关资源
      最近更新 更多