Examples

View the position of vehicles on the map

Used functions

Used technologies and tools

Source code

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

// Map identifier on Mapbox.com
$map_id = '****';

$client = new \SoapClient('http://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 href="https://api.tiles.mapbox.com/mapbox.js/v1.6.2/mapbox.css" rel="stylesheet">
        <script src="https://api.tiles.mapbox.com/mapbox.js/v1.6.2/mapbox.js"></script>
    </head>
    <body>
        <div id="map" style="height: 500px;"></div>
        
        <script>
        var map = L.mapbox.map('map', '<?php echo $map_id; ?>').setView([49.599983, 17.2386716], 10);
        // data for a map in JSON format - without array keys
        var data = <?php echo json_encode(array_values($cars)); ?>;
        
        if (data.length > 0)
        {
            var features = [];
            for (var i = 0; i < data.length; i++)
            {
                // point (car) on the map
                features[i] = {
                    type: 'Feature',
                    properties: {
                        // the content of the label associated with the point (car) on the map: car registration number and current speed
                        title: "Registration number: " + data[i].identifier + "<br>Speed: " + data[i].speed + " km/h",
                    },
                    geometry: {
                        type: 'Point',
                        coordinates: [parseFloat(data[i].longitude), parseFloat(data[i].latitude)]
                    }
                };
            }

            var geoJson = {
                type: 'FeatureCollection',
                features: features
            };

            // insert points (cars) into the map
            var cars = L.mapbox.featureLayer(geoJson).addTo(map);

            // show label on mouse over (car)
            cars.on('mouseover', function(e) {
                e.layer.openPopup();
            }).on('mouseout', function(e) {
                e.layer.closePopup();
            });

            map.on('ready', function() {
                // make all cars visible
                map.fitBounds(cars.getBounds(), {
                    animate: false,
                    padding: L.point(5,5)      
                });
            });
        }
        </script>
    </body>
</html>

List of vehicles with details