【问题标题】:Worklight Geofence Error Dynamically not updating the triggersWorklight 地理围栏错误动态不更新触发器
【发布时间】:2016-05-08 16:13:22
【问题描述】:

在 Worklight 中实施地理围栏。第一次触发地理围栏时,它工作正常。用户离开该区域后,我尝试再次更新触发器和 startAcquisition,但在这种情况下,触发器不会被触发。

//display the position to the user
function displayPosition(pos) {
    $('#longitude').html('<b>Longitude:</b> ' + pos.coords.longitude);
    $('#latitude').html('<b>Latitude:</b> ' + pos.coords.latitude);
    $('#timestamp').html('<b>Timestamp:</b> ' + new Date(pos.timestamp));
}

function alertOnGeoAcquisitionErr(geoErr) {
    alert('Error acquiring geolocation (' + geoErr.code + '): ' + geoErr.message);
}
var triggers={};
    var geoPolicy = WL.Device.Geo.Profiles.LiveTracking();
    geoPolicy.timeout = 60000; // set timeout to 1 minute
    geoPolicy.maximumAge = 10000; // allow to use a position that is 10 seconds old


function getFirstPositionAndTrack() {
    alert('Click OK to proceed to acquire starting position');

    // use GPS to get the user's location

    // note: to see at high-accuracy, change RoughTracking above to LiveTracking

    // get the user's current position
    WL.Device.Geo.acquirePosition(
            function(pos) {

                displayPosition(pos);

                 triggers = generaTriggers(pos);

                startTrace({ Geo: geoPolicy },triggers,{ Geo: alertOnGeoAcquisitionErr })
            },
            function(geoErr) {
                alertOnGeoAcquisitionErr(geoErr);
                // try again:
                getFirstPositionAndTrack();
            },
            geoPolicy
        ); 
}

function onConnectSuccess(){
    // start up acquisition process
    getFirstPositionAndTrack();

}

function onConnectFailure(){


getFirstPositionAndTrack();

}

function wlCommonInit(){
    // Common initialization code goes here
    WL.Client.connect({
        onSuccess: onConnectSuccess,
        onFailure: onConnectFailure
    });

    // keep running while in background on Android; will show a notification
    WL.App.setKeepAliveInBackground(true);
}

function generaTriggers(pos){

    var triggers = {
          Geo: {
            posChange: { // display all movement
              type: "PositionChange",
              callback: function(deviceContext) {
                  displayPosition(deviceContext.Geo);
                }
            },

            leftArea: { // alert when we have left the area
              type: "Exit",
              circle: {
                longitude: pos.coords.longitude,
                latitude: pos.coords.latitude,
                radius: 200
              },
              callback: function(deviceContext) {
                alert('Left the area');
                //resetTriggers(deviceContext.Geo);
                startTrace({ Geo: geoPolicy },resetTriggers(deviceContext.Geo),{ Geo: alertOnGeoAcquisitionErr });
                //WL.Client.transmitEvent({ event: 'exit area'}, true);
              }
            },

            dwellArea: { // alert when we have stayed in the vicinity for 3 seconds
              type: "DwellInside",
              circle: {
                longitude: pos.coords.longitude,
                latitude: pos.coords.latitude,
                radius: 50
              },
              dwellingTime: 3000,
              callback: function() {
                alert('Still in the vicinity');
                //WL.Client.transmitEvent({ event: 'dwell inside area'}, true);
              }
            }
          } 
        };


    return triggers;
}

function resetTriggers(deviceContext){

//alert();
return generaTriggers(deviceContext);

}

function startTrace(policy, trigger, geoerr){
alert("trace");
                WL.Device.startAcquisition(policy, trigger, geoerr );

}

【问题讨论】:

    标签: ibm-mobilefirst geofencing


    【解决方案1】:

    在调用 startAcquisition 之间维护每个触发器的状态;它依靠触发器名称来识别唯一配置,而不是检查底层配置是否不同。

    一般来说你有两个选择。

    1. 对于要更新的​​触发器,请务必使用与当前触发器集不同的名称。请注意,如果存在应维护其内部状态的现有触发器(例如,设备是否已在配置区域中),则需要维护旧名称。
    2. 对 startAcquisition 进行额外的中间调用,传入一个触发器对象,其中删除了您要更新的所有触发器(因此,删除它们),然后最终调用将使用原始名称(因为新配置将是应用)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-01
      • 2015-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多