如何自动检查位置?

时间:2017-03-24 15:19:14

标签: javascript cordova firebase ionic-framework

我在我的Ionic应用程序中使用Geofire和Cordova Geolocation:

//Catch the position of the user
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {

        var posOptions = {timeout: 10000, enableHighAccuracy: true};
   $cordovaGeolocation
   .getCurrentPosition(posOptions)
   .then(function (position) {
      var lat  = position.coords.latitude
      var long = position.coords.longitude

       var userId = firebase.auth().currentUser.uid;
// Insert geofire location of userId
var firebaseRef = firebase.database().ref('/geolocation/');
var geoFire = new GeoFire(firebaseRef);
geoFire.set(userId, [lat, long]).then(function() {

}, function(error) {

});
// end geofire
         firebase.database().ref('accounts/' + userId).update({
       lat: lat,
       long: long,
       })
   }, function(err) {

   });
   var watchOptions = {timeout : 3000, enableHighAccuracy: true};
   var watch = $cordovaGeolocation.watchPosition(watchOptions);
   watch.then(
      null,
      function(err) {

      },
      function(position) {
         var lat  = position.coords.latitude
         var long = position.coords.longitude

         var userId = firebase.auth().currentUser.uid;
         firebase.database().ref('accounts/' + userId).update({
       lat: lat,
       long: long,
       })
      }
   );
    watch.clearWatch();
    }

当用户移动时,我尝试让应用自动捕获该位置。例如,用户移动几米并使用此代码更新它。

然而,我找不到这样的方法。我做错了吗?

0 个答案:

没有答案