Google Maps Angular循环回调以显示多个标记

时间:2015-02-09 21:41:13

标签: angularjs google-maps callback google-maps-markers ionic

我正在尝试在地图上显示标记,但是我遇到了困难,我在使用回调方面不是很熟练,有人可以帮助我谢谢吗?我想要多个标记来显示不同游戏的位置。

我正在使用rest api从db中检索游戏详细信息,我知道这有用,就在我检索它们之后,我再也无法访问刚刚检索到的数据了。

我正在使用离子(Angular),我相信可能有更好的方法。使用循环的最佳方法是什么?如果有人能指出我如何改进我的代码。这是我的地图控制器

.controller('MapController', function($scope, $ionicLoading, gameFactory) {
  $scope.initialise = function() {
...//set up map 
    });
$scope.map = map;
};
      $scope.showGameMarkers = function() {
    var map = document.getElementById("map");
        gameFactory.getGames().success(function(data) {
        //at this stage the data variable contains the details of my game
           angular.forEach(data, function(key, data) {
           //now data is empty
        var latLng = new google.maps.LatLng(data.locationLatitude, data.locationLongitude); 
        console.log("latlang = "+latLng);
        // Creating a marker and putting it on the map
        var marker = new google.maps.Marker({
            position: latLng,
            title: data.locationName,
            setMap : map
        });
      });
  }).error(function(data) {
      console.log("no games");
  });

  };
google.maps.event.addDomListener(document.getElementById("map"), 'load', $scope.initialise());
google.maps.event.addDomListener(document.getElementById("map"), 'load', $scope.showGameMarkers());
});

提前谢谢!

1 个答案:

答案 0 :(得分:0)

看起来你的forEach循环可能会反转你的键和数据参数。根据{{​​3}},它应该是

angular.forEach(values, function(value, key) {
    ...
}