Oum*_*lek 3 service location angularjs cordova ionic
我正在尝试获取当前用户位置,但是当我重新加载代码时它会发生变化,即使我想每隔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
})
Run Code Online (Sandbox Code Playgroud)
但这只有在我启动应用程序时才有效,请有人有个主意吗?帮助我同步我的应用程序每x秒获取位置数据:)
可能你想要的是这个:
app.controller('GeoCtrl', function($cordovaGeolocation, $interval) {
var posOptions = {
timeout: 10000,
enableHighAccuracy: false
};
var getLocation = function() {
$cordovaGeolocation
.getCurrentPosition(posOptions)
.then(function(position) {
// location coordinates
console.log(position.coords);
})
.catch(function(err) {
// handle error
console.log(err)
});
};
$interval(getLocation, 3000); // 3 seconds for now
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
796 次 |
最近记录: |