我正在使用此cordova插件https://github.com/christocracy/cordova-plugin-background-geolocation/作为ios应用,但遇到了一些问题。如果有人可以提供帮助,我将不胜感激。
以下是我从插件示例本身使用的示例代码。
问题是当检测到位置变化时,回调函数(callbackFn)似乎没有被调用,但是我确实在Iphone上收到通知,我看到其中一个通知是因为突出的bg-task被强行杀死了。不知道究竟是什么意思......
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady: function() {
app.receivedEvent('deviceready');
if (window.plugins.backgroundGeoLocation) {
app.configureBackgroundGeoLocation();
}
},
// Update DOM on a Received Event
receivedEvent: function(id) {
window.cache.clear( function(){console.log("cache cleared");}, function(){console.log("cache could not be cleared");} );
require(['http://localhost:8080/sdm/app_resources/app_product_HY6I1FJ5KGRD/resources/js/main.js']);
},
configureBackgroundGeoLocation: function() {
// Your app must execute AT LEAST ONE call for the current position via standard Cordova geolocation,
// in order to prompt the user for Location permission.
window.navigator.geolocation.getCurrentPosition(function(location) {
console.log('Location from Phonegap');
});
var bgGeo = window.plugins.backgroundGeoLocation;
/** This would be your own callback for Ajax-requests after POSTing background geolocation to your server.*/
var yourAjaxCallback = function(response) {
bgGeo.finish();
};
/**This callback will be executed every time a geolocation is recorded in the background.*/
var callbackFn = function(location) {
console.log('[js] BackgroundGeoLocation callback: ' + location.latitudue + ',' + location.longitude);
// Do your HTTP request here to POST location to your server.
deviceId = 'I2FKRKXX8SQ0';
trackingToken = 'SomeSecurityToken';
jsonData={location:{longitude:location.longitude,latitude:location.latitude}};
var encodedToken = Base64.encode('tracker' + ':' + trackingToken);
var http = new XMLHttpRequest();
var url = "http://localhost:8080/sdm/v1/KC/host/device/"+deviceId;
var params = JSON.stringify(jsonData);
http.open("PUT", url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/json");
http.setRequestHeader("accept", "json");
http.setRequestHeader("Authorization", "Basic "+encodedToken);
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
/* http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}*/
http.send(params);
yourAjaxCallback.call(this);
};
var failureFn = function(error) {
console.log('BackgroundGeoLocation error');
}
// BackgroundGeoLocation is highly configurable.
bgGeo.configure(callbackFn, failureFn, {
desiredAccuracy: 10,
stationaryRadius: 20,
distanceFilter: 30,
activityType: 'AutomotiveNavigation',
debug: true, // <-- enable this hear sounds for background-geolocation life-cycle.
stopOnTerminate: false
});
// Turn ON the background-geolocation system. The user will be tracked whenever they suspend the app.
bgGeo.start();
// If you wish to turn OFF background-tracking, call the #stop method.
// bgGeo.stop()
}
};
答案 0 :(得分:4)
如果您愿意,可以尝试我的后台插件,它使用ios和android的纯javascript回调。
https://github.com/pmwisdom/cordova-background-geolocation-services
答案 1 :(得分:0)
检查您是否使用了android。除非你有高级版的Christrocracy插件,否则它不会执行你的回调函数。您必须为要为Android设备发送的位置指定url
。
答案 2 :(得分:-1)
您还必须在开始时添加回调函数和失败函数。我有同样的问题。
例如。配置:bgGeo.configure(callbackFn, failureFn...
bgGeo.start();
必须更改为bgGeo.start(callbackFn, failureFn);