Skip to main content

Vehicle tracking

Maps can be used to show real time positions of vehicles. In this example we create a 3D version of the Flightradar24 app. For this we need a script to collect real-time flight information from the OpenSky Network API. The script collects data in JSON format and a connector is used to create and update topics. The airplane is displayed as a 3D solid with a 2D label and a form is created to show the real-time data when the user clicks on an airplane.

Script

To collect information about all aircraft in a geographical area you can use the free OpenSky API. Note that if you do not have an API key there is a rate limit on the number of requests. In this example the geographical area is hardcoded, but it could also be defined by the object input parameter.

const get = async (object, context) => {
let begin = Math.ceil(new Date().getTime() / 1000) - 100000
let end = begin + 100000
url = `https://opensky-network.org/api/states/all?lamin=51.0&lomin=5.1&lamax=51.6&lomax=5.5`
let response = await http.get(url)
let result = []
for (let state of response.states) {
result.push({

icao24: state[0],
callsign: state[1].trim(),
velocity: state[9],
latitude: state[6],
longitude: state[5],
altitude: state[7],
track: state[10]
})
}
return result
}

Solid

To display a 3D model of a plane we create a new solid in programming mode. The solid has one parameter callsign and uses the label function to draw a 2D label on top of the 3D screen. You can enter any HTML text in the label. In this example it is just the call sign of the aircraft that is imported from OpenSky.

Connector

Create a new secret called OpenSky and set the type to script and set the script to OpenSky. Enter the name of the secret as the data source in the connector.

Form

When you click on one of the airplanes on the map you can show a form with the real time data. The form definition below shows how you can extract the altitude from the position field, using the JavaScript split function. The altitude is converted from meters to feet. The speed in m/s is converted to knots.

Airplane

callsign
<callsign
|12

altitude
<+position.split(',')[2]*3.28084
[0000 ft]
|4

speed
<speed*1.94
[000 kt]
|4

track
<Math.round(heading)
[000 deg]
|4