【问题标题】:Ionic & ngCorgova GeolocationIonic & ngCorgova 地理定位
【发布时间】:2015-11-19 14:09:57
【问题描述】:

我目前正在从事 Ionic / ngCordova 地理定位项目,我们希望能够获取用户当前位置。

我已设法获取用户位置,但无法获得超时刷新以在移动时继续查看他们的位置。

任何建议/帮助将不胜感激。

   (function(angular, undefined) {
      "use strict";
      var app = angular.module('starter');

      app.controller('geoCtrl', function($cordovaGeolocation, $scope) {
        var self = this;

        var posOptions = {enableHighAccuracy: true, maximumAge: 5000, timeout: 8000};
        $cordovaGeolocation
          .getCurrentPosition(posOptions)
          .then(function (position) {
            var lat  = position.coords.latitude;
            var long = position.coords.longitude;
            self.location = lat +' - '+ long;
            console.log(self.location);
          }, function(err) {
            console.log('error:', error);
          });

        var watchOptions = {
          timeout : 1000,
          enableHighAccuracy: false
        };
        var watch = $cordovaGeolocation.watchPosition(watchOptions);

        watch.then(
          null,
          function(err) {
          },
          function(position) {
            var lat  = position.coords.latitude;
            var long = position.coords.longitude;
            self.location = lat +' - '+ long;
            console.log(self.location);
        });

        watch.clearWatch();

      });
    })(angular);

【问题讨论】:

    标签: geolocation ionic-framework ionic ngcordova


    【解决方案1】:

    您正在调用watch.clearWatch(),它的作用基本上是重置观察者。

    根据插件的documentation

    navigator.geolocation.clearWatch

    停止观察由 watchID 参数。

    navigator.geolocation.clearWatch(watchID);

    只需评论该行,一切都会正常。

    【讨论】:

      猜你喜欢
      • 2022-01-26
      • 2018-05-30
      • 2018-09-19
      • 1970-01-01
      • 2021-04-15
      • 2018-03-07
      • 2018-03-31
      • 1970-01-01
      • 2018-04-17
      相关资源
      最近更新 更多