我正在尝试使用Cordova cordova-plugin-geofence插件。我的测试设备是Google Nexus 5(Android)。我按照文档(https://github.com/cowbell/cordova-plugin-geofence)中的步骤进行操作。它还说我需要在我的设备上使用Google Play服务。我有(8.1.15)。
我
也许我忘了什么,但如果我这样做,请告诉我。我编辑的唯一文件是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);
});
};
}
我收到了两个警报(成功和Geofence成功添加)。
答案 0 :(得分:2)
尝试使用示例应用https://github.com/cowbell/ionic-geofence
此插件也使用Google Geofence API https://developer.android.com/training/location/geofencing.html,并且它不准确到几米的水平。尝试增加半径,3米不足以捕捉任何过渡,尝试用100代替。
为了测试我正在使用Lockito Fake GPS,为了使其正常工作,您需要在设备上进行位置模拟。