我正在工作灯中实现地理围栏,但我想动态创建地理触发器。我看到了这篇文章并尝试了她的代码,但它对我不起作用。我使用了假位置并输入了地理围栏,但未触发回调函数。这是我的代码。branch_data_g是一个包含经度和纬度的 json 字符串。
function getFirstPositionAndTrack() {
WL.Logger.info("getfirstpositionandtrack function");
// use GPS to get the user's location
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
// note: to see at high-accuracy, change RoughTracking above to LiveTracking
// get the user's current position
WL.Device.Geo.acquirePosition(
function(pos) {
WL.Logger.debug("acquired position");
WL.Logger.debug("Longitude: " + pos.coords.longitude);
WL.Logger.debug("Latitude: " + pos.coords.latitude);
var triggers = new Object();
triggers.Geo = {};
var trigger_events = generateTrigger();
triggers.Geo = trigger_events;
WL.Device.startAcquisition({ Geo: geoPolicy }, triggers, { Geo: alertOnGeoAcquisitionErr } );
},
function(geoErr) {
alertOnGeoAcquisitionErr(geoErr);
// try again:
getFirstPositionAndTrack();
},
geoPolicy.Geo
);
}
function generateTrigger() {
var parsed_data = JSON.parse(branch_data_g);
WL.Logger.info("generatetrigger function: " + parsed_data.branches.length);
var trigger_events = new Object();
WL.Logger.info("sample: " + parsed_data.branches[0].latitude);
for(var i = 0; i < parsed_data.branches.length; i++) {
var trigger = {
type: "Enter",
circle: {
longitude: parsed_data.branches[i].longitude,
latitude: parsed_data.branches[i].latitude,
radius: 100
},
callback: function() {
WL.Logger.info("Enter branch");
WL.Client.transmitEvent({ branch: "enter branch"}, true);
}
};
trigger_events["branch"+i] = trigger;
}
return trigger_events;
}
这是我的适配器中的代码:
function eventHandler() {
//nothing to do here..
}
WL.Server.setEventHandlers([
WL.Server.createEventHandler({branch: 'enter branch'}, eventHandler)
]);
我错过了什么吗?提前致谢!