我想在数组angular.js中推送纬度和经度对象

时间:2016-11-07 15:10:06

标签: angularjs ionic-framework

我想在angular.js中的数组中推入纬度和经度对象,请帮助我。

$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();
  };

2 个答案:

答案 0 :(得分:1)

  

我想在数组中推送纬度和经度对象

function(position) {
  var array = [];
  var object = {
      lat:position.coords.latitude,
      long:position.coords.longitude
  };
  array.push(object);
  console.log(array[0].lat + '' + array[0].long);
}

或直接

function(position) {
  var array = [];
  array.push(position.coords);
  console.log(array[0].latitude + '' + array[0].longitude);
}

这是你问题的答案,但也许你还没有为你的目的正确地提出问题。

答案 1 :(得分:1)

你可以这样做。

$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();
  };