navigator.geolocation.getCurrentPosition() 锁屏时
navigator.geolocation.getCurrentPosition() when screen is locked
我有一个应用程序,它在旅行模式下会不断轮询位置。我的问题是,一旦屏幕被锁定,应用程序就无法再从 phone 访问地理定位。
我设法找到了这个插件,但它需要我购买它才能在 Android 中工作。
http://shop.transistorsoft.com/pages/cordova-background-geolocation-premium
有谁知道是否有一个免费的选项,我可以使用它来获取位置,以便在屏幕锁定时成功地在 Ionic / Cordova 应用程序中进行轮询?
你看过NG-Cordova了吗?
首先将 ng-cordova 添加到您的项目中:
bower install ngCordova
or
<script src="lib/ngCordova/dist/ng-cordova.js"></script>
<script src="cordova.js"></script>
然后注入:
angular.module('myApp', ['ngCordova'])
这是一个您可以尝试的插件:
http://ngcordova.com/docs/plugins/backgroundGeolocation/
只需安装插件:
cordova plugin add https://github.com/christocracy/cordova-plugin-background-geolocation.git
然后将其绑定到控制器:
module.controller('MyCtrl', function($scope, $cordovaBackgroundGeolocation) {
var options = {
// https://github.com/christocracy/cordova-plugin-background-geolocation#config
};
document.addEventListener("deviceready", function () {
// `configure` calls `start` internally
$cordovaBackgroundGeolocation.configure(options)
.then(
null, // Background never resolves
function (err) { // error callback
console.error(err);
},
function (location) { // notify callback
console.log(location);
});
$scope.stopBackgroundGeolocation = function () {
$cordovaBackgroundGeolocation.stop();
};
}, false);
});
另一种选择是在 Android 上使用 partial wakelock 让您的应用在后台(屏幕关闭或退出前台)时保持活动状态。您需要通过插件来执行此操作,但它与后台服务具有相同的效果,让您的应用程序保持活动状态以在后台接收位置更新。
有关 Cordova 2.0 插件的源代码,请参阅 my old answer here(它需要为 Cordova 3+ 更新)。
我有一个应用程序,它在旅行模式下会不断轮询位置。我的问题是,一旦屏幕被锁定,应用程序就无法再从 phone 访问地理定位。
我设法找到了这个插件,但它需要我购买它才能在 Android 中工作。 http://shop.transistorsoft.com/pages/cordova-background-geolocation-premium
有谁知道是否有一个免费的选项,我可以使用它来获取位置,以便在屏幕锁定时成功地在 Ionic / Cordova 应用程序中进行轮询?
你看过NG-Cordova了吗?
首先将 ng-cordova 添加到您的项目中:
bower install ngCordova
or
<script src="lib/ngCordova/dist/ng-cordova.js"></script>
<script src="cordova.js"></script>
然后注入:
angular.module('myApp', ['ngCordova'])
这是一个您可以尝试的插件: http://ngcordova.com/docs/plugins/backgroundGeolocation/
只需安装插件:
cordova plugin add https://github.com/christocracy/cordova-plugin-background-geolocation.git
然后将其绑定到控制器:
module.controller('MyCtrl', function($scope, $cordovaBackgroundGeolocation) {
var options = {
// https://github.com/christocracy/cordova-plugin-background-geolocation#config
};
document.addEventListener("deviceready", function () {
// `configure` calls `start` internally
$cordovaBackgroundGeolocation.configure(options)
.then(
null, // Background never resolves
function (err) { // error callback
console.error(err);
},
function (location) { // notify callback
console.log(location);
});
$scope.stopBackgroundGeolocation = function () {
$cordovaBackgroundGeolocation.stop();
};
}, false);
});
另一种选择是在 Android 上使用 partial wakelock 让您的应用在后台(屏幕关闭或退出前台)时保持活动状态。您需要通过插件来执行此操作,但它与后台服务具有相同的效果,让您的应用程序保持活动状态以在后台接收位置更新。
有关 Cordova 2.0 插件的源代码,请参阅 my old answer here(它需要为 Cordova 3+ 更新)。