无法让 Cordova cordova-plugin-geofence 插件工作
Can't get the Cordova cordova-plugin-geofence Plugin to work
我正在尝试让 Cordova cordova-plugin-geofence 插件正常工作。我的测试设备是 Google Nexus 5 (Android)。我遵循了文档中的步骤 (https://github.com/cowbell/cordova-plugin-geofence)。它还说我的设备上需要 Google Play 服务。我有那个 (8.1.15).
我
- 将插件安装到干净的 PhoneGap 项目中。
- 在设备就绪事件后初始化插件。
- 添加了地理围栏并开始监听地理围栏转换。
- 进出地理围栏,但什么也没发生。
也许我忘记了什么,但如果忘记了请告诉我。我编辑的唯一文件是 index.js。从那个文件我编辑了 onDeviceReady 和 receivedEvent:
onDeviceReady: function() {
app.receivedEvent('deviceready');
window.geofence.initialize(function() {
alert('success');
}, function(err) {
alert(err);
});
},
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
//add geofence
window.geofence.addOrUpdate({
id: 'abc',
latitude: 51.808944,
longitude: 4.667041,
radius: 3,
transitionType: 3,
notification: {
id: 1,
title: 'Welcome',
text: 'Hello',
smallIcon: 'file://img/icon.png',
icon: 'file://img/logo.png',
openAppOnClick: false,
vibration: [2000],
data: {
name: 'foo',
lastName: 'bar'
}
}
}).then(function() {
console.log('Geofence successfully added');
alert('Geofence successfully added');
}, function(reason) {
console.log('Adding geofence failed', reason);
alert('Adding geofence failed: ' + reason);
});
//listen for geofences
window.geofence.onTransitionReceived = function(geofences) {
geofences.forEach(function(geo) {
console.log('Geofence transition detected', geo);
alert('Geofence transition detected: ' + geo);
});
};
}
我收到了两个警报(成功和地理围栏添加成功)。
尝试使用示例应用程序 https://github.com/cowbell/ionic-geofence
另外这个插件使用了Google地理围栏APIhttps://developer.android.com/training/location/geofencing.html,它并不精确到几米的水平。尝试增加半径,3 米不足以捕获任何过渡,请改为尝试 100。
为了测试,我正在使用 Lockito Fake GPS,为了使其正常工作,您需要允许在您的设备上进行位置模拟。
我正在尝试让 Cordova cordova-plugin-geofence 插件正常工作。我的测试设备是 Google Nexus 5 (Android)。我遵循了文档中的步骤 (https://github.com/cowbell/cordova-plugin-geofence)。它还说我的设备上需要 Google Play 服务。我有那个 (8.1.15).
我
- 将插件安装到干净的 PhoneGap 项目中。
- 在设备就绪事件后初始化插件。
- 添加了地理围栏并开始监听地理围栏转换。
- 进出地理围栏,但什么也没发生。
也许我忘记了什么,但如果忘记了请告诉我。我编辑的唯一文件是 index.js。从那个文件我编辑了 onDeviceReady 和 receivedEvent:
onDeviceReady: function() {
app.receivedEvent('deviceready');
window.geofence.initialize(function() {
alert('success');
}, function(err) {
alert(err);
});
},
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
//add geofence
window.geofence.addOrUpdate({
id: 'abc',
latitude: 51.808944,
longitude: 4.667041,
radius: 3,
transitionType: 3,
notification: {
id: 1,
title: 'Welcome',
text: 'Hello',
smallIcon: 'file://img/icon.png',
icon: 'file://img/logo.png',
openAppOnClick: false,
vibration: [2000],
data: {
name: 'foo',
lastName: 'bar'
}
}
}).then(function() {
console.log('Geofence successfully added');
alert('Geofence successfully added');
}, function(reason) {
console.log('Adding geofence failed', reason);
alert('Adding geofence failed: ' + reason);
});
//listen for geofences
window.geofence.onTransitionReceived = function(geofences) {
geofences.forEach(function(geo) {
console.log('Geofence transition detected', geo);
alert('Geofence transition detected: ' + geo);
});
};
}
我收到了两个警报(成功和地理围栏添加成功)。
尝试使用示例应用程序 https://github.com/cowbell/ionic-geofence
另外这个插件使用了Google地理围栏APIhttps://developer.android.com/training/location/geofencing.html,它并不精确到几米的水平。尝试增加半径,3 米不足以捕获任何过渡,请改为尝试 100。
为了测试,我正在使用 Lockito Fake GPS,为了使其正常工作,您需要允许在您的设备上进行位置模拟。