我正在尝试获取当前用户位置,但每次重新加载代码时位置都会发生变化,即使我想每隔x秒获取一次位置。这是我的解决方案:
app.controller('geoCtrl', function($scope, $timeout, $cordovaBackgroundGeolocation, $ionicPlatform, $window, $cordovaGeolocation)
{
var posOptions = {timeout: 10000, enableHighAccuracy: false};
$cordovaGeolocation
.getCurrentPosition(posOptions)
.then(function (position) {
var lat = position.coords.latitude
var long = position.coords.longitude
alert('lat='+lat);
alert('long='+long);
}, function(err) {
// error
});
var watchOptions = {
timeout : 3000,
enableHighAccuracy: false // may cause errors if true
};
var watch = $cordovaGeolocation.watchPosition(watchOptions);
watch.then(
null,
function(err) {
// error
},
function(position) {
var lat = position.coords.latitude
var long = position.coords.longitude
});
watch.clearWatch();
//-- End Geolocal
})
但这仅适用于我启动应用程序时,有人有想法吗?帮助我同步我的应用程序以每x秒获取位置数据 :)