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());
});
谢谢你提前!