0

I am developing an Micro finance Angular Application in that i am trying to locate the location of agent who login to my application...and i storing the details in collection(ActivityDetails)...But the requirement is quite different...like once the agent is logged in to our application means every One Hour the location should be tracked and stored in collection(ActivityDetails)...for this i used set interval function....it execute successfully but when i navigate to different controller the function is not executing because i write location tracking and storing code in SignIn Controller when it navigate to diff.. controller i am facing the problem ...How can i make these function globally that run successfully once logged in and irrespective of any controller..please help me out to find solution for this....and i am new to Angular just i have 3 month of experience... below i am adding the function.. please verify and guide me..Thank You..

///////Signin Controller code for location tracking//////////

     $scope.nearme = function(firstname,activity,date,time) {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function (position) 
          {
            lat = position.coords.latitude; 
            lng = position.coords.longitude;
            var latlng = new google.maps.LatLng(lat, lng);
            var geocoder = geocoder = new google.maps.Geocoder();
            geocoder.geocode({ 'latLng': latlng }, function (results, status) 
                {
                if (status == google.maps.GeocoderStatus.OK) 
                {
                    if (results[1]) 
                    {
                      console.log(results[1].formatted_address)
                      console.log(firstname)
                      console.log(date)
                      console.log(time);
                      var obj={};
                      obj["User"]=firstname;
                      obj["Activity"]=activity;
                      obj["Date"]=$scope.date;
                      obj["Time"]=$scope.time;
                      obj["LocationDetails"]=results[1].formatted_address;

                      console.log(obj)
                   $http.post("/ncrActivityDetails",obj).success(function(res9){
                        console.log(res9)
                      })
                    }
                }
            });

        });  
    }
}
/////////////setInterval Function for calling every one hour///////////

    setInterval(function(){
    $scope.date3 = {date1:new Date()}
    $scope.activity="Tracking";
     var  dates  = new Date(((new Date($scope.date3.date1).toISOString().slice(0, 23))+"-05:30")).toISOString();
            var a = dates.split("T");
            $scope.date = a[0];
            $scope.time = a[1];
            console.log($scope.date)
            console.log($scope.time)
    $scope.nearme($scope.firstname,$scope.activity,$scope.date,$scope.time);
},3600000);

////////////////ActivityDetails collection sample data/////////

    {
    "_id" : ObjectId("5a8a6936fa642a14149a12d1"),
    "User" : "7829780519",
    "Activity" : "Login",
    "Date" : "2018-02-19",
    "Time" : "11:35:26.777Z",
    "LocationDetails" : "Sri laxmivenkateshwara, #1/10, 4th Main, Srinivasanagar, B S K 3rd Stage, Water Tank Rd, Banashankari 3rd Stage, Banashankari, Bengaluru, Karnataka 560085, India"
}
Liam
  • 27,717
  • 28
  • 128
  • 190
  • write a service/factory for this. Transfer your function there as `this.nearme = function(firstname,activity,date,time){...}` and call it in your controller with `YourService.nearme(...)`. (Also you are using `$scope.date` in your function, whereas you are passing `date` as a parameter) – Aleksey Solovey Feb 20 '18 at 14:03

1 Answers1

0

solutions: 1) create your function in your .run module so that it will work eventhough you have navigated to different controllers , check this link

.run(function($interval){
   setInterval(function(){
$rootScope.nearme($scope.firstname,$scope.activity,$scope.date,$scope.time);},3600000);});

2) create a service - link

Justinjs
  • 130
  • 7
  • It's a [bad practice to pollute your `$rootScope`](https://stackoverflow.com/questions/35345446/best-practice-for-using-rootscope-in-an-angularjs-application). It's better to use a service. – Aleksey Solovey Feb 20 '18 at 14:15