PHP code example of automapa / map-services

1. Go to this page and download the library: Download automapa/map-services library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

automapa / map-services example snippets


use Automapa\MapServices\ApiClient;
use Automapa\MapServices\Config;

$client = new ApiClient(new Config(
    key: 'YOUR_API_KEY',
    pass: 'YOUR_PASSWORD',
));

$client->session()->generateSession();

$response = $client->geocoder()->geocode(
    city: 'Warsaw',
    street: 'Domaniewska',
    house: '37',
    maxResults: 1,
);
$results = $response->data(); // array of results, each with x, y, city, street, ...

$response = $client->geocoder()->geocodemulti(
    addresses: [
        ['city' => 'Warsaw', 'street' => 'Domaniewska', 'house' => '37'],
        ['city' => 'Kraków', 'street' => 'Floriańska',  'house' => '1'],
    ],
);
$results = $response->data(); // array of result arrays, one per input address

$response = $client->geocoder()->revgeocode(
    point: [21.0073642, 52.18288],
    snapToBld: true,
);
$address = $response->data(); // city, street, house, pcode, ...

$response = $client->geocoder()->revgeocodemulti(
    points: [
        [21.0073642, 52.18288],
        [19.9449799, 50.0646501],
    ],
    snapToBld: true,
);
$addresses = $response->data(); // array of address arrays, one per input point

$response = $client->autocomplete()->search(
    query: 'Warsaw Domaniewska 37',
    // types: ['place'],  // optional: 'place', 'poi', or both (null = both)
);

$items = $response->data()['items'];

foreach ($items as $item) {
    echo $item['name'];        // "Domaniewska 37, 02-672 Warsaw"
    echo $item['type'];        // "place"
    echo $item['subtype'];     // "building"
    echo $item['coords']['x']; // longitude (lng)
    echo $item['coords']['y']; // latitude (lat)
}

$response = $client->autocomplete()->searchAddress(
    query: [
        'city'    => 'Warsaw',
        'street'  => 'Dom',     // partial street name
        'number'  => '',
        'zipcode' => '02-672',
    ],
    source: 'street',             // field being searched: 'place', 'street', 'number', 'zipcode'
    selected: ['city', 'zipcode'],  // optional: fields already confirmed by the user
    // limit: 10,                  // optional: max results (default 10)
);

$items = $response->data()['items'];
// [['city' => 'Warsaw', 'street' => 'Domaniewska', 'zipcode' => '02-672', 'count' => 1]]

$response = $client->routes()->route(
    points: [
        [21.0073642, 52.2297],  // Warsaw (x=lng, y=lat)
        [18.6282,    54.3520],  // Gdańsk
    ],
    route: [
        'type'    => 'short',  // 'quick' | 'short' | 'optimal'
        'traffic' => true,     // true = take live traffic into account
    ],
    object: ['type' => 'car'],
);

$data = $response->data();
echo round($data['length'] / 1000, 1) . ' km';
echo gmdate('H:i:s', $data['eta']);   // estimated travel time
echo $data['mapurl'];                  // link to map
// $data['desc']  — turn-by-turn manoeuvre list
// $data['tolls'] — toll charges

$response = $client->routes()->matrix(
    points: [
        [21.0073642, 52.2297],
        [18.6282,    54.3520],
        [16.9252,    52.4064],
    ],
    type: 1,  // 1 = time (minutes), 2 = distance (metres)
);

foreach ($response->data() as $row) {
    echo "Segment {$row['points']}: "
        . round($row['length'] / 1000) . ' km, '
        . $row['duration'] . " min\n";
}

$response = $client->routes()->optimize(
    points:   [[21.0, 52.2], [18.6, 54.4], /* ... */],
    type:     1,      // 1 = time, 2 = distance
    fixedEnd: false,  // true = last point is the destination; false = open end
    object:   ['type' => 'car'],
);

$data = $response->data();
$data['order'];    // optimal index sequence, e.g. "0,3,2,1"
$data['sections']; // segment details: x, y, eta, dist

// Step 1: submit — each point optimizeQueue(
    points:     [['id' => 'Warsaw', 'x' => 21.0, 'y' => 52.2], /* ... */],
    optimizeBy: 'time',  // 'time' or 'distance'
    object:     ['type' => 'car'],
);
$requestId = $response->data()['requestId'];

// Step 2: poll for the result
do {
    sleep(2);
    $result = $client->routes()->optimizeQueueResult(requestId: $requestId);
    $data   = $result->data();
} while ($data['resultReady'] !== true);

$data['optimizedSequence']; // array of point IDs in the optimal order

// Road segment information nearest to a point
$response = $client->road()->getSegmentInfo(
    x: 21.00736,
    y: 52.18288,
    distLimit: 50,        // search radius in metres (optional)
    useTolls: 1,          // 1=accept tolls, 0=reject, -1=ignore (optional)
    useHighways: -1,      // 1=accept, 0=reject, -1=ignore (optional)
);
$segment = $response->data()['segment'];   // id, streetName, speedLimit, isToll, …

// Speed limit for a single point
$response = $client->road()->speedCheck(
    x: 15.2154636,
    y: 52.3118895,
    dir: 350,             // direction of travel in degrees (optional)
);
$speedLimit      = $response->data()['speedLimit'];       // cars
$speedLimitTruck = $response->data()['speedLimitTruck'];  // trucks

// Speed limits for up to 100 points in one request
$response = $client->road()->speedCheckMulti(
    points: [
        ['x' => 15.2154636, 'y' => 52.3118895, 'dir' => 350],
        ['x' => 13.8412285, 'y' => 52.3111549],
    ],
);
foreach ($response->data() as $result) {
    echo $result['speedLimit'] . "\n";
}

use Automapa\MapServices\Endpoints\RoadPermit;

$rp = $client->roadPermit(RoadPermit::ROAD_PERMIT_ENVIRONMENT_TEST);

// GeoJSON Polygon (ring must be closed — first === last point)
$poly = [
    'type'        => 'Polygon',
    'coordinates' => [[
        [21.000, 52.220],
        [21.020, 52.220],
        [21.020, 52.240],
        [21.000, 52.240],
        [21.000, 52.220],
    ]],
];

// Create a new permit
$response = $rp->add(
    permissions:           ['tr_tonnage' => 12, 'tr_tonn_axis' => 8.5],
    poly:                  $poly,
    date_start:            '2026-01-01',
    date_end:              '2026-12-31',
    description:           'Weight limit — city centre',
    document_name:         'DOC-001',
    location:              'Warsaw',
    is_valid_after_expiry: false,
    extra:                 ['myCustomField' => 'value'], // forwarded to the API as-is
);
$id = $response->raw()['result']['id'];

// Replace an existing permit
$rp->overwrite(
    id:          $id,
    permissions: ['tr_tonnage' => 24],
    poly:        $poly,
);

// List all permits (optionally filter)
$permits = $rp->list(id: $id);   // $permits->data() — array of permit objects

// Delete a permit
$rp->remove($id);

$points = [
    ['x' => 21.007, 'y' => 52.230, 't' => 1700000000],
    ['x' => 21.012, 'y' => 52.234, 't' => 1700000060],
    // each point: x = longitude, y = latitude, t = Unix timestamp
];

$response  = $client->speed()->speedReport(
    objID:    'VEHICLE-001',
    objName:  'Van 1',
    objGroup: 'Fleet',
    points:   $points,
);
$requestID = $response->data()['requestID'];

do {
    sleep(5);
    $result = $client->speed()->speedReportResult(requestID: $requestID);
    $data   = $result->data();
} while ($data['progress'] < 1.0);

foreach ($data['overspeedingArray'] as $violation) {
    echo $violation['startLocation'] . ' → ' . $violation['endLocation'] . "\n";
    echo '  speed: ' . $violation['speedMax'] . ' km/h, limit: ' . $violation['speedLimit'] . " km/h\n";
}
// $data['reportURL'] — link to the full report

// Autocomplete
$client->autocomplete()->search(
    query: 'Warsaw',
    extra: ['myCustomParam' => 'value'],
);

// Speed (named params override $extra when the same key appears in both)
$client->speed()->speedReport(
    objID: 'VEHICLE-001',
    objName: 'Van 1',
    objGroup: 'Fleet',
    points: $points,
    extra: ['myCustomParam' => 'value'],
);

// Via service + method name (preferred)
$client->call('Geocoder', 'newMethod', ['param' => 'value']);

// Or via full URL path (fallback)
$client->request('POST', '/v3/Geocoder/newMethod', ['param' => 'value']);

// Native format (default)
$response = $client->geocoder()->geocode(city: 'Kraków');
$data = $response->data();   // API data
$raw  = $response->raw();    // full raw response (always available)

// Google format
$response = $client->geocoder()->geocode(city: 'Kraków', extra: ['format' => 'google']);

use Automapa\MapServices\Exception\AuthenticationException;
use Automapa\MapServices\Exception\AutomapaException;

try {
    $response = $client->geocoder()->geocode(city: 'Warsaw');
} catch (AuthenticationException $e) {
    echo 'Authentication error: ' . $e->getMessage();
} catch (AutomapaException $e) {
    echo 'API error: ' . $e->getMessage();
}