Toggle navigation
Wialon Playground
Add library
jQuery latest
Bootstrap 3.3.1
Wialon Javascript SDK
Run
Get code
Fork
Save
Examples
Login
Get units
Change unit icon
Get messages
Get sensors
Edit sensors
Commands
Monitoring notification
Track layer
Get resources
Resources and accounts
Account parameters
Do payment
Management Driver
Create notification
Get geofences
Geofence parameters
Create geofence
Gurtam map
Units on map
Unit trace
Create report template
Execute report
Execute custom report
Create driver
Bind driver to unit
Unit edit fields
Import fillings
Token login for site
Advanced authorization form
Token usage in app
Account hierarchy
Nearest units
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Wialon Playground - Forum 11659</title> <script type="text/javascript" src="//code.jquery.com/jquery-latest.min.js"></script> <script type="text/javascript" src="//hst-api.wialon.com/wsdk/script/wialon.js"></script> </head> <body> <table><tbody id="table"></tbody></table> <hr> <div id='log'></div> </body> </html>
HTML
#log { border: 1px solid #c6c6c6; }
CSS
/* * https://forum.gurtam.com/viewtopic.php?id=11659 */ // Token that will be used for auth // For more info about how to generate token check // http://sdk.wialon.com/playground/demo/app_auth_token var TOKEN = '5dce19710a5e26ab8b7b8986cb3c49e58C291791B7F0A7AEB8AFBFCEED7DC03BC48FF5F8'; // Print message to log function msg(text) { $("#log").prepend(text + "<br/>"); } // will be called after DOM is ready and we are successfully logged in function init() { var session = wialon.core.Session.getInstance(); session.loadLibrary('resourceZones'); var unitFlags = wialon.item.Item.dataFlag.base | wialon.item.Unit.dataFlag.lastPosition, resourceFlags = wialon.item.Item.dataFlag.base | wialon.item.Resource.dataFlag.zones; session.updateDataFlags([ {type: 'type', data: 'avl_unit', flags: unitFlags, mode: 0}, {type: 'type', data: 'avl_resource', flags: resourceFlags, mode: 0} ], onDataFlagsUpdate); // Units that we want to check agains zones var unitToZone = { '717361': {resourceId: 717351, zoneId: 4}, '734422': {resourceId: 717351, zoneId: 169} }; function onDataFlagsUpdate(error) { if (error) { msg(wialon.core.Errors.getErrorText(error)); return; } wialon.core.Remote.getInstance().startBatch("get_zones_in_point"); for (var unitId in unitToZone) if (unitToZone.hasOwnProperty(unitId)) { (function() { var unit = session.getItem(unitId); if (!unit) { msg('No such unit: ' + unitId); return; } var $tr = jQuery('<tr>'), $nameTd = jQuery('<td>').text(unit.getName()), $zoneTd = jQuery('<td>').text('NO ZONE'), $valueTd = jQuery('<td>').text('Loading...'); $tr.append($nameTd).append($zoneTd).append($valueTd); jQuery("#table").append($tr); var pos = unit.getPosition(); if (!pos) { msg('Unit ' + unitId + ' has no position'); $valueTd.text('NO POS'); return; } var unitZone = unitToZone[unitId]; var resource = session.getItem(unitZone.resourceId); if (!resource) { msg('No such resource: ' + unitZone.resourceId); $valueTd.text('NO RESOURCE'); return; } var zone = resource.getZones()[unitZone.zoneId]; if (!zone) { msg('No such zone: ' + unitZone.resourceId + '-' + unitZone.zoneId); $valueTd.text('NO ZONE'); return; } $zoneTd.text(zone.n); var requestZoneId = {}; requestZoneId[unitZone.resourceId] = [zone.id]; // Do a request to check that unit is in the geofence wialon.util.Helper.getZonesInPoint({ lat: pos.y, lon: pos.x, zoneId: requestZoneId }, function(error, data) { if (error) { msg(wialon.core.Errors.getErrorText(error)); $valueTd.text('ERROR'); return; } if (data && Array.isArray(data[unitZone.resourceId]) && data[unitZone.resourceId].length > 0) { // in geofence $valueTd.text('YES'); } else { // not in geofence var distance = wialon.util.Geometry.getDistance(pos.y, pos.x, zone.b.cen_y, zone.b.cen_x); $valueTd.text('NO ' + distance + ' meters'); } }); })(); } wialon.core.Remote.getInstance().finishBatch(function(){}, "get_zones_in_point"); } } // execute when DOM is ready $(document).ready(function () { // init session wialon.core.Session.getInstance().initSession("https://hst-api.wialon.com"); // For more info about how to generate token check // http://sdk.wialon.com/playground/demo/app_auth_token // try to login wialon.core.Session.getInstance().loginToken(TOKEN, "", function (code) { // login callback // if error code - print error message if (code){ msg(wialon.core.Errors.getErrorText(code)); return; } msg("Logged successfully"); // when login suceed then run init() function init(); }); });
JS
Result
Source code of example
Close ✕
×
Source code