From 562610b62d8a8348aceda0c25105f9cf2c16fc1a Mon Sep 17 00:00:00 2001 From: "kuldeep.arora" Date: Tue, 19 Dec 2017 15:42:17 +0530 Subject: [PATCH] angular interval and state change code --- .../flightTracking/flightTracking.controller.js | 113 +++++++++++++-------- 1 file changed, 70 insertions(+), 43 deletions(-) diff --git a/app/partials/flightTracking/flightTracking.controller.js b/app/partials/flightTracking/flightTracking.controller.js index f55d1d1..12b0cba 100644 --- a/app/partials/flightTracking/flightTracking.controller.js +++ b/app/partials/flightTracking/flightTracking.controller.js @@ -2,7 +2,7 @@ angular.module('acufuel') -.controller('flightTrackingController', ['$scope','$compile', 'uiCalendarConfig', 'flightTrackingService', function($scope, $compile, uiCalendarConfig, flightTrackingService) { +.controller('flightTrackingController', ['$scope','$compile', 'uiCalendarConfig', 'flightTrackingService','$interval', function($scope, $compile, uiCalendarConfig, flightTrackingService,$interval) { $scope.flightInfo = {}; var map; var icon = "https://en.spitogatos.gr/visualCaptcha/images/airplane.png"; @@ -12,54 +12,78 @@ angular.module('acufuel') $scope.initialise = function() { var mapProp = { - center: new google.maps.LatLng(36.778259, -98.417931), //LLANDRINDOD WELLS + center: new google.maps.LatLng(36.778259, -98.417931), //US center zoom: 4, mapTypeId: google.maps.MapTypeId.ROADMAP }; + //getting fligts one time on instant pageload + getFlights(); map = new google.maps.Map(document.getElementById("map"), mapProp); console.log('sssssssss', json); - setInterval(function(){ - $.get(json, function(json1) { - $.each(json1, function(key, data) { - var exists = false; - $.each(arr, function (index, value) { - if(value.title === data.id) { - var latLng = new google.maps.LatLng(data.latitude, data.longitude, data.altitude); - value.link.setPosition(latLng); - exists = true; - } - }); - if(!exists) { - var latLng = new google.maps.LatLng(data.latitude, data.longitude, data.altitude); - - var marker = new google.maps.Marker({ - position: latLng, - map: map, - icon: icon, - title: data.id - }); - - var details = "Aircraft :" + " " + data.id + "
" + - "Path :" + " " + data.departurePoint + " " + "--->" + " " + data.arrivalPoint + "
" + - "Altitude :" + " " + data.altitude + " " + "ft" + "
" + - "Speed :" + " " + data.speed + " " + "Knots" + "
" + - "Departure Time :" + " " + data.departureActualTime + " " + "(Actual)" + "
" + - "Arrival Time :" + " " + data.arrivalEstimatedTime + " " + "(Estimated)" + "
"; - - arr.push({ - title: data.id, - link: marker, - details : details - }); - - bindInfoWindow(marker, map, infowindow, details, data); - } - }); - }); - },5000); - } + + + var stopflight=$interval(function(){ + console.log('====interval start======='); + //getting flights on each interval + getFlights(); + },4000); + + + /*---Stop flights in case state change---*/ + $scope.$on("$destroy",function(){ + console.log('====live data stop-====='); + if (angular.isDefined(stopflight)) { + $interval.cancel(stopflight); + } +}); + +} + + /*---Get live flight data function---- */ + + var getFlights=function(){ + $.get(json, function(json1) { + $.each(json1, function(key, data) { + var exists = false; + $.each(arr, function (index, value) { + if(value.title === data.id) { + var latLng = new google.maps.LatLng(data.latitude, data.longitude, data.altitude); + value.link.setPosition(latLng); + exists = true; + } + }); + if(!exists) { + var latLng = new google.maps.LatLng(data.latitude, data.longitude, data.altitude); + + var marker = new google.maps.Marker({ + position: latLng, + map: map, + icon: icon, + title: data.id + }); + + var details = "Aircraft :" + " " + data.id + "
" + + "Path :" + " " + data.departurePoint + " " + "--->" + " " + data.arrivalPoint + "
" + + "Altitude :" + " " + data.altitude + " " + "ft" + "
" + + "Speed :" + " " + data.speed + " " + "Knots" + "
" + + "Departure Time :" + " " + data.departureActualTime + " " + "(Actual)" + "
" + + "Arrival Time :" + " " + data.arrivalEstimatedTime + " " + "(Estimated)" + "
"; + + arr.push({ + title: data.id, + link: marker, + details : details + }); + + bindInfoWindow(marker, map, infowindow, details, data); + } + }); + }); +} + + function bindInfoWindow(marker, map, infowindow, strDescription, data) { google.maps.event.addListener(marker, 'click', function() { @@ -83,7 +107,10 @@ angular.module('acufuel') $('.left-panel').animate({width: '0', padding: '0'}); }) - google.maps.event.addDomListener(document.getElementById("map"), 'load', $scope.initialise()); + + + + google.maps.event.addDomListener(document.getElementById("map"), 'load', $scope.initialise()); -- 2.0.0