Umer Waqas Umer Waqas - 1 month ago 13
AngularJS Question

I want to push latitude and longitude object in an array angular.js

I want to push latitude and longitude object in an array in angular.js please help me.

$scope.showPosition = function showPosition() {
var watchOptions = {timeout : 3000, enableHighAccuracy: false};
var watch = $cordovaGeolocation.watchPosition(watchOptions);

watch.then(
null,
function(err) {
console.log(err)
},
function(position) {
var lat = position.coords.latitude;
var long = position.coords.longitude;
$scope.lat = lat;
$scope.long = long;
console.log(lat + '' + long)
}
);
// watch.clearWatch();
};

Answer

You can do something like this.

$scope.GeoLocationArray = [];
$scope.showPosition = function showPosition() {
    var watchOptions = {timeout : 3000, enableHighAccuracy: false};
    var watch = $cordovaGeolocation.watchPosition(watchOptions);

    watch.then(
        null,
        function(err) {
          console.log(err)
        },
        function(position) {
          var lat  = position.coords.latitude;
          var long = position.coords.longitude;
          $scope.lat = lat;
          $scope.long = long;
          $scope.GeoLocationArray.push({
            latitude: lat,
            longitude: long
          })
          console.log(lat + '' + long)
        }
    );
    // watch.clearWatch();
  };