on{X}: 偵測經緯度與使用emit模擬觸發事件

要查出目前位置的經緯度也可以使用on{X}來求出;用device.location建立一個位置的Listener,當Listener偵測到位置變動時觸發changed事件:

 
// create GPS listener with update interval of 5 sec
// you can also use GPS or CELL or PASSIVE providers here
var listener = device.location.createListener('CELL', 5000); 

// regiter on location changed
listener.on('changed', function (signal) {
  // on receiving location print it and stop the provider
  console.info('緯度: '+signal.location.latitude+
    '經度: '+signal.location.longitude);
  listener.stop();
});

// start the GPS listener
listener.start();

location changed

用emit測試事件的觸發

前一篇以device.regions的"enter"與"exit"事件來判斷進入或離開特定位置,因為這兩個事件只在位置有變動時才會被觸發,為了方便測試可以使用emit( )方法來模擬觸發事件:

 
// 模擬進入指定的經緯度位置
device.regions.emit('enter',{"latitude" : 25.049845,"longitude" : 121.580291});

// 下面則是模擬離開指定的經緯度位置
device.regions.emit('exit',{"latitude" : 25.049845,"longitude" : 121.580291});

區分訊息的發送者

為了確認某個rule確實有被執行,我們經常會以device.notefications.createNotification()發送訊息到通知面板,為了能清楚辨識訊息是由那個rule發出的,建議在訊息開頭加上rule的名稱,這樣就能很清楚知道訊息是由誰發出的。

device.notifications.createNotification("[Wifi updated] 3G mode is " + 
    device.network.mobileDataEnabled).show();

##

您可能也會有興趣的類似文章

簡睿

服務於軟體業的資訊老兵。興趣廣泛,學習力佳,樂於分享所知所學。

您可能也會喜歡…

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *