离子地理位置:每1分钟进行一次表面检查

时间:2015-12-19 07:49:59

标签: android cordova parse-platform geolocation ionic

在我的离子应用程序中,我需要记录gps cordinates来解析核心后端。

  var opts={
        enableHighAccuracy:true,
        timeout: 8000,
        maximumAge: 5000
    };
    var watch = $cordovaGeolocation.watchPosition(opts).then(function () { /* Not  used */
           },
           function (err) {

           }, function (position) {
             var lat  = position.coords.latitude;
             var long = position.coords.longitude;
             $scope.slat=lat;
             $scope.slong=long;
             var uob=JSON.parse(localStorage.getItem('meemp'));
             console.log(uob);
             var point = new Parse.GeoPoint({latitude: lat, longitude: long});
             console.log(point);
             console.log(uob["objectId"]);
             $timeout(function() {
               var PeopleObject = Parse.Object.extend("PeopleObject");
               var person = new PeopleObject();
               person.set("lat", point);
               person.set("uid", uob["objectId"]);
               person.save(null, {});
             },30000);

           });

上面的代码检查位置的任何变化,并将geopoint和objectid添加到解析核心。但只要有变化,就会发生这种情况。我需要每1秒记录一次变化。每次gps协调更改时,上面的代码都会记录下来。所以有时它每分钟记录30-40个变化..我每分钟只需要一次输入.. 我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

您可以使用getCurrentPosition()获取单个位置,然后按固定间隔调用它。像这样:

var opts = {
    enableHighAccuracy: true,
    timeout: 8000,
    maximumAge: 5000
};
var timer = setInterval(function(){
    $cordovaGeolocation.getCurrentPosition(opts).then(function () { /* Not  used */
   },
   function (err) {

   }, function (position) {
     var lat  = position.coords.latitude;
     var long = position.coords.longitude;
     $scope.slat=lat;
     $scope.slong=long;
     var uob=JSON.parse(localStorage.getItem('meemp'));
     console.log(uob);
     var point = new Parse.GeoPoint({latitude: lat, longitude: long});
     console.log(point);
     console.log(uob["objectId"]);
     $timeout(function() {
       var PeopleObject = Parse.Object.extend("PeopleObject");
       var person = new PeopleObject();
       person.set("lat", point);
       person.set("uid", uob["objectId"]);
       person.save(null, {});
     },30000);

   });
}, 1000);