I am developing an angularJS application where my geocordinates from the device are taken and sent to my REST service every few seconds. Code for geo coordinates sending is located in just one controller. It means that geolocation is only updated in my server if the template page for that controller is open. How do I make it so that geolocation is always updated regardless of which page I am on.
Here is my code for that controller:
.controller('geolocationCtrl', [ '$scope', '$http', 'geolocationFactory', function($scope, $http, geolocationFactory) {
// constants should be uppercase var GET_PATH = 'https://temp-domain.co.uk/rest/rest.php/client/geolocation', PUT_PATH = 'https://temp-domain.co.uk/rest/rest.php/client/geolocation/update'; $http.get(GET_PATH).then(function (response) { var respLong = Number(response.data.longitude), respLat = Number(response.data.latitude); $scope.map = drawMap(respLong, respLat); $scope.marker = new google.maps.Marker({ position: new google.maps.LatLng(respLong, respLat) }); $scope.marker.setMap($scope.map); }, function (errorResponse) { console.error('error response ' + errorResponse); }); function drawMap(latitude, longitude) { var lat = latitude, long = longitude, currentLatlng = new google.maps.LatLng(lat, long); var map = new google.maps.Map(document.getElementById('map'), { center: currentLatlng, zoom: 10 }); return map; }; setInterval(function () { geolocationFactory.getCurrentPosition().then(function(location) { var newLatitude = location.coords.latitude, newLongitude = location.coords.longitude; // delete old marker $scope.marker.setMap(null); $scope.marker = new google.maps.Marker({ position: { lat: newLatitude, lng: newLongitude } }); var LatLng = new google.maps.LatLng(newLatitude, newLongitude); $scope.map.setCenter(LatLng); $scope.marker.setMap($scope.map); // send data to server $http.put(PUT_PATH, { "longitude": newLatitude, "latitude": newLongitude }).then(function (response) { console.log('data sended'); }, function (errorResponse) { console.error(errorResponse); }); }.bind(this)); }, 2000); }])