Wialon Geofence API: Create, Edit and Query Zones

Reference · last checked

How the Wialon geofence API works: resource/update_zone for create, edit and delete, reading zones back, point lookups, groups and the entry/exit trigger.

The Wialon geofence API is a handful of commands on the resource service, and the official reference splits them across six separate pages with different naming (the API says “zone”, the interface says “geofence”, the SDK samples say both). This page puts the whole loop in one place: create a geofence, read it back, edit or delete it, ask which geofences contain a point, group geofences, and fire a notification when a unit enters or leaves one. Where the reference is unclear, the text says so instead of guessing.

If the request format is new to you, read the Postman setup first. Everything below is a call to wialon/ajax.html with svc and params, authenticated with a session id from the token page.

Geofences belong to a resource

A geofence is a subitem of a resource, like drivers and notifications. Every command takes the resource id as itemId, and a geofence id is only meaningful together with it. The reference makes this visible in resource/get_zones_by_point, which takes zones as {"<resource id>":[<geofence id>, ...]}, and in resource/get_zone_data, where id is described as the zone id inside the resource’s zone array. Two resources can both have a geofence with the same id, so never store a bare geofence id.

Two limits from the Wialon help center matter for integrations that create zones in bulk:

  • No more than 31,744 micro-objects of each type (geofences, notifications, drivers and so on) per resource. Geofences in the trash count toward it.
  • No more than 5000 points per geofence if you want to be able to edit it. A geofence saved from a track is capped at 10,000 points and split into several geofences beyond that.

Create a geofence

Create, edit and delete all go through one command, resource/update_zone. A polygon around a depot:

https://hst-api.wialon.com/wialon/ajax.html?sid=<sid>&svc=resource/update_zone&params={"itemId":<resourceId>,"id":0,"callMode":"create","n":"Depot North","d":"Main yard","t":2,"w":0,"f":32,"c":1090453504,"tc":0,"ts":12,"min":0,"max":18,"p":[{"x":27.5612,"y":53.9006,"r":0},{"x":27.5701,"y":53.9006,"r":0},{"x":27.5701,"y":53.8951,"r":0},{"x":27.5612,"y":53.8951,"r":0}]}
NameMeaning
itemIdResource id, the geofence’s parent
idGeofence id; 0 creates
callModecreate, update, delete or reset_image
n, dName and description
tType: 1 line, 2 polygon, 3 circle
wLine thickness or circle radius
fFlags, see below
cColour
tc, tsLabel text colour and font size
min, maxShow on the map from this zoom level, up to this zoom level
path, libIdShort path to the default icon and the icon library id (0 is the default library)
jpCustom JSON
pPoints: x is longitude, y is latitude, r is radius
oldItemId, oldZoneIdResource and geofence to copy from

Things the reference leaves vague, and what to do about them:

  • Which parameters are optional. The page lists every field and says only that the others are required for create and update. The example above sends the full set, which is the safe choice. Trim it only after you have seen your account accept the shorter version.
  • Colour format. resource/get_zone_data and resource/create_zone_by_track describe the colour as ARGB, while the update_zone response block says “color RGB”. The example value is a semi-transparent red under the ARGB reading. Create one geofence in the Wialon interface with the colour you want, read it back with get_zone_data, and copy the integer you get.
  • Circles. The reference describes w as “line thickness or circle radius” and also lists r on each point, but does not say which one carries a circle’s radius on create. Do the same trick: draw a circle in the interface, read it back, and mirror the shape.
  • Polygon closure. Nothing says whether the first point must be repeated at the end. Read back a polygon drawn in the interface to see what the server stores.

The flag values for f are listed on the get_zone_data page: 0x10 is “mandatory for using parameter line width”, 0x20 shows the shape, 0x40 means do not simplify. As everywhere in this API, JSON carries decimal numbers, so 0x20 goes in as 32. For a line geofence, set the 0x10 bit together with w, for example f of 48 (0x10 plus 0x20).

On create and update the call returns [<geofenceId>, {...}] with the full stored record: name, type, flags, colour, a bounding box b and a creation and modification time ct and mt. Read the record rather than assuming your input was stored verbatim.

Edit, copy and delete

The same call, the same parameters, a different callMode:

  • update with the real id and the full new shape. Send the whole definition, including p, not only the field you changed.
  • delete needs only itemId, id and callMode. It returns [<id>, null].
  • reset_image removes a geofence’s custom image. Uploading one is a separate multipart call, resource/upload_zone_image.
  • To copy a geofence, pass oldItemId and oldZoneId naming the source. The reference says this also lets you edit the copy in the same call, which suits cloning a standard zone template across resources.

List geofences without downloading every point

The geofences of a resource arrive through core/search_items with the geofence data flag. Flag 0x1000 is 4096, and 0x100000 (1048576) is geofence groups:

svc=core/search_items&params={"spec":{"itemsType":"avl_resource","propName":"sys_name","propValueMask":"*","sortType":"sys_name"},"force":1,"flags":4096,"from":0,"to":0}

The resource data format page shows what comes back: a zl object keyed by sequence number, with n, d, id, f, t, a checksum e and the bounding box b, plus zlmax, the maximum geofence count (-1 is unlimited). There are no points in that list. It is a summary for rendering: the bounding box is enough to draw a marker or filter by area, and the e checksum is the natural field to compare when you check what changed.

To get points, area and perimeter, ask for specific geofences with resource/get_zone_data:

svc=resource/get_zone_data&params={"itemId":<resourceId>,"col":[<id1>,<id2>],"flags":29}

col lists geofence ids and flags picks the blocks of the response:

FlagDecReturns
0x011Area (ar)
0x022Perimeter (pr)
0x044Boundary and centre (b)
0x088All points (p)
0x1016Basic properties

The documented default is 0x1C, which is 28: boundary, all points and basic properties, no area or perimeter. The example above adds area with 29. A sensible pattern for a sync job: list with 4096, compare the e checksums against your stored copy (the reference only calls it a CRC16 check sum, so verify that it changes when you edit a shape), and call get_zone_data only for the ids that changed. The rate-limit-safe sync design covers why that matters once you have thousands of zones.

If you work in JavaScript, the SDK playground has a ready Get geofences sample built on resource.getZones(). The SDK page covers when the SDK is worth loading and when plain ajax.html calls are enough.

Find which geofence contains a point

resource/get_zones_by_point answers “where is this coordinate” without downloading any shapes:

svc=resource/get_zones_by_point&params={"spec":{"lat":53.8980,"lon":27.5650,"radius":100,"zoneId":{"<resourceId>":[]}}}

zoneId is {"<resource id>":[<geofence id>, ...]}, and an empty array means all geofences of that resource. radius is the search radius in metres. The response is a list with one object per resource, mapping each geofence id to a distance in metres: 0 when the point is inside, a positive distance when it is outside. That makes the same call a nearest-geofence lookup. The reference does not say whether geofences beyond radius are left out of the result, so filter on the distance yourself instead of relying on it.

This is the call to use for reverse lookups on stored positions: take a unit’s last position from the units page pattern and ask which yard it is in. For a time series, it is cheaper to let the server do the work through a report or a notification than to call this per message.

Create a geofence from a track

resource/create_zone_by_track turns a unit’s track into a line geofence:

svc=resource/create_zone_by_track&params={"layerName":"<trackLayerName>","itemId":<resourceId>,"unitId":<unitId>,"n":"Route 12","c":1090453504,"w":30}

The response is {"all_zones":<uint>,"created_zone":<uint>}. The reference explains all_zones as the number of new geofences, with 10,000 points to one, which matches the help center limit above: a long track becomes several geofences, so check all_zones instead of assuming one. The reference does not explain what layerName refers to beyond “track layer name”, so this call is easiest to use when the track layer already exists in the session.

Geofence groups

Groups are a separate subitem with their own command, resource/update_zones_group, and the same call modes minus reset_image:

svc=resource/update_zones_group&params={"itemId":<resourceId>,"id":0,"callMode":"create","n":"Customer yards","d":"","zns":[101,102,103],"f":0}

zns is the array of geofence ids, and f is documented as not used at the moment. The response is [<groupId>, {"id":..,"n":..,"d":..,"zns":[..]}]. You read groups back with core/search_items and the flag 1048576, which returns a zg object and a zgmax limit.

Fire a notification on entry or exit

Wialon has no separate “geofence alert” object. Entering and leaving a geofence is a trigger on an ordinary notification, created with resource/update_notification exactly as described on the notifications page. The geofence part is the trg value. In the reference’s notification data format, the control type is geozone:

"trg":{"t":"geozone","p":{"geozone_ids":"101,102","type":0}}

geozone_ids is a comma-separated string of geofence ids, and type is 0 to control entries and 1 to control exits. The reference block also lists min_speed, max_speed, include_lbs, lo and a group of sensor fields. The sensor fields look carried over from another control type. The safest approach is to create one geofence notification in the interface, read it back with get_notification_data, and copy its trg shape rather than building it from the printed block.

Geofence ids are only unique within a resource, and the reference does not say how geozone_ids handles another resource’s geofences, so keep the geofences and the notification on the same resource.

What breaks

The reference has no error list specific to geofence calls, so the general error codes apply, and these are the usual causes:

  • Error 4 (invalid input). The parameter shape for the call mode is wrong. For delete, send only itemId, id and callMode. For create and update, send the full definition, including p as an array of objects.
  • Error 7 (no rights). The session has no rights on the resource. Check the token’s access flags on the token page, and on the resource itself.
  • A Geofences tab that is missing in the interface. The help center says the Geofences: Access service must be enabled in the account or billing plan. If geofence calls misbehave on one account and work on another, check that service first.
  • A geofence that “does not exist”. The id came from a different resource. Qualify every id with its resource.

Where next

Geofence data feeds the notification engine for entry and exit alerts and the report execution behind visit and dwell-time reports. If you use geofences as business objects such as customers, depots or delivery zones, treat Wialon as the place they are drawn and your own database as the place they are keyed: sync the e checksums on a schedule, store the resource id with every geofence id, and diff locally.

Also in this reference

Let's connect

  • “Our client needed a data pipeline. It came back working, plus a few Wialon fixes we had not asked for. That client trusts us more now.”
    Faiz K. Customer Manager · Trakpro Limited