2015-10-13 64 views
2

我正在試圖讓Cordova cordova-plugin-geofence插件正常工作。我的測試設備是Google Nexus 5(Android)。我遵循文檔中的步驟(https://github.com/cowbell/cordova-plugin-geofence)。它還表示我需要在我的設備上使用Google Play服務。我有(8.1.15)。無法讓Cordova cordova-plugin-geofence插件正常工作

  • 插件安裝到一個乾淨的PhoneGap項目。
  • 在deviceready事件之後初始化插件。
  • 添加了地理圍欄並開始監聽地理圍欄過渡。
  • 走進和離開地理圍欄,但沒有發生任何事情。

也許我忘記了一些東西,但如果我確實請告訴我。我編輯的唯一文件是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); 
     }); 
    }; 
} 

我收到的兩個警報(成功,併成功添加地理柵欄)。

回答

2

嘗試使用示例應用程序https://github.com/cowbell/ionic-geofence

而且這個插件使用谷歌API地理圍欄和https://developer.android.com/training/location/geofencing.html是不準確的數米的水平。嘗試增加半徑,3米是不夠的,以捕捉任何過渡,嘗試用100代替。

對於測試,我使用Lockito Fake GPS,爲了使其工作,您需要允許在您的設備上進行位置模擬。

+0

我把半徑增加到了30米。然後,我離開了半徑再回到它,但我沒有收到通知。我也使用模擬位置。之後,我嘗試了您的示例應用程序。我得到了死亡的白色屏幕。當進一步調查爲什麼我得到死亡白屏時,我看到我錯過了模塊烤麪包機。控制檯錯誤:https://gyazo.com/b843825c2505e3d2a60db3058715e34b – SPottuit

+0

我看到缺角的toster。你是否在開始應用程序之前運行'bower install'和'npm install'? – tsubik

+0

您應該考慮的警告也很少。首先,如果您的應用程序未在Android的前臺運行(但如果設置了geofence中的通知對象,您應該獲得本地通知),則不會獲得任何JavaScript代碼回調。第二個30米仍然可能是太小的半徑,這個插件只是使用谷歌的API,但不是微博。 – tsubik