Reputation: 3060
I am adding a loading_gif to my google maps and using google.maps.event.addListener() to determine when the map is done loading and update a variable to stop the loading spinner. It looks like everything is working as intended and I am seeing the variable get updated but the spinner still displays.
My loading spinner:
<div style="position:absolute;z-index:99;top:85px;left:38%;" ng-if="mapLoader==0">
<p style="text-align:center;">
LOADING MAP...
<br><img style="height:100px;" ng-src="img/ajax_loading.gif">
</p>
</div>
The app starts, the loading starts, the map loads in the background but the spinner never goes away. In console, I see all the console messages printing out - including the one that indicates the map has finished loading and setting the $scope.mapLoader = 1
which should stop the DIV with the loading gif to hide....but it doesn't
My controller:
.controller('MapCtrl', function($scope,$ionicModal,$ionicPopover,$ionicSlideBoxDelegate,$rootScope,$state,constants,apiService) {
$scope.mapLoader = 0 ;
$scope.mapTrigger = 0 ;
// recursive function to test for maps finished loading
//setMap is a global variable set elsewhere.
function checkLoader() {
console.log("CHECKING LOADER") ;
if ($scope.mapLoader == 0) {
if (setMap && $scope.mapTrigger == 0) {
$scope.mapTrigger = 1 ;
google.maps.event.addListenerOnce(setMap,'tilesloaded', (function() {
console.log("\tMAP LOADED") ; // These two successfully print out
$scope.mapLoader = 1 ; // this should stop the DIV from displaying.
console.log($scope.mapLoader) ; // These two successfully print out
})) ;
}
setTimeout(function() {
console.log("\tChecking Map") ;
checkLoader() ; // repeat function again
},500) ; // 1/2 second intervals
}
}
if ($scope.mapLoader == 0) {
checkLoader() ;
} else {
$scope.mapLoader = 1 ;
}
})
Printed out to console:
CHECKING LOADER
Checking Map
CHECKING LOADER
Checking Map
CHECKING LOADER
Checking Map
CHECKING LOADER
MAP LOADED
1
Checking Map
CHECKING LOADER
Upvotes: 0
Views: 620
Reputation: 1909
Do it like so
google.maps.event.addListenerOnce(setMap,'tilesloaded', (function() {
$scope.$apply(function() {
$scope.mapLoader = 1;
});
})) ;
You have to tell AngularJS that you have some events, that are not tracked by Angular, and you need to apply changes that were done in a moment.
Read more for $apply
here and here.
Upvotes: 1