2015-02-09 31 views
0

我想在地圖上顯示標記,但我遇到了困難,我沒有太熟練的回調的使用,有人可以幫我出來感謝?我想要多個標記來顯示不同遊戲的位置。谷歌地圖通過回調的角度循環顯示多個標記

我使用休息api從數據庫檢索遊戲細節,我知道這是有效的,它只是在我檢索它們之後,我不能再訪問剛纔檢索的數據。

我正在使用離子(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()); 
}); 

謝謝你提前!

回答