Examples

View the position of vehicles on the map

Used functions

Used technologies and tools

Source code

<?php
// login data
$kodf = '****';
$username = '****';
$pass = '****';

$client = new \SoapClient('https://api.webdispecink.cz/code/WebDispecinkServiceNet.php?wsdl');

// the _getAllCarsPosition() function returns array of objects containing position of cars for entered user
// Use the _getCarsPosition() function to display only position of one vehicle on the map
// the same function is recommended if you are interested in a small percentage of the total number of vehicles in the company
$car_positions = $client->_getAllCarsPosition($kodf, $username, $pass);

// array of cars to show on the map
$cars = array();

if (isset($car_positions->item) &&
    is_array($car_positions->item))
{
    foreach ($car_positions->item as $position)
    {
        // show only cars with valid coordinates
        if ($position->latitude != 0 ||
            $position->longitude != 0)
        {
            // Insertion of latitude, longitude and speed
            $cars[$position->carid] = array(
                'latitude'  => $position->latitude,
                'longitude' => $position->longitude,
                'speed' => $position->speed,
            );
        }
    }

    // the _getCarsList () function returns also a car registration number
    $car_list = $client->_getCarsList($kodf, $username, $pass);
    if (isset($car_list->item) &&
        is_array($car_list->item))
    {
        foreach ($car_list->item as $car)
        {
            if (isset($cars[$car->carid]))
            {            
                $cars[$car->carid]['identifier'] = $car->identifikator;     // car registration number
            }
        }
    }
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        
        <title>Car positions on map - API Webdispatching</title>
        
        <link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A==" crossorigin="">
        <script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js" integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA==" crossorigin=""></script>
    </head>
    <body>
        <div id="map" style="height: 500px;"></div>
        
        <script>
        // data for a map in JSON format - without array keys
        var data = <?php echo json_encode(array_values($cars)); ?>;
        
        // create map
        var map = L.map('map').setView([49.599983, 17.2386716], 13);

        // add tile layer
        L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
            attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
        }).addTo(map);

        if (data.length > 0)
        {
            var markers = [];
            for (var i = 0; i < data.length; i++)
            {
                // create marker
                markers[i] = L.marker([parseFloat(data[i].latitude), parseFloat(data[i].longitude)]).bindTooltip('Registration number: <b>' + data[i].identifier + '</b><br>Speed: ' + data[i].speed + ' km/h');
            }
        }

        // add markers to map and fit bounds to show all
        map.fitBounds(L.featureGroup(markers).addTo(map).getBounds());
        </script>
    </body>
</html>

List of vehicles with details