PHP code example of cyrildewit / php-maps-urls

1. Go to this page and download the library: Download cyrildewit/php-maps-urls 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/ */

    

cyrildewit / php-maps-urls example snippets


use CyrildeWit\MapsUrls\MapsUrl;
use CyrildeWit\MapsUrls\Actions\Directions;
use CyrildeWit\MapsUrls\Actions\Search;
use CyrildeWit\MapsUrls\Actions\StreetViewPanorama;
use CyrildeWit\MapsUrls\Enums\TravelMode;
use CyrildeWit\MapsUrls\Coordinates;

$searchUrl = MapsUrl::for(new Search(query: 'Rijksmuseum'));
// https://www.google.com/maps/search/?api=1&query=Rijksmuseum

$directionsUrl = MapsUrl::for(new Directions(
    origin: 'Amsterdam',
    destination: 'Utrecht',
    travelMode: TravelMode::Bicycling,
));
// https://www.google.com/maps/dir/?api=1&origin=Amsterdam&destination=Utrecht&travelmode=bicycling

$panoramaUrl = MapsUrl::for(new StreetViewPanorama(
    viewpoint: new Coordinates(48.857832, 2.295226),
));
// https://www.google.com/maps/@?api=1&map_action=pano&viewpoint=48.857832%2C2.295226

use CyrildeWit\MapsUrls\MapsUrl;
use CyrildeWit\MapsUrls\Actions\Search;

$searchUrl = MapsUrl::for(new Search(query: 'Eindhoven, Nederland'));
// https://www.google.com/maps/search/?api=1&query=Eindhoven%2C+Nederland

use CyrildeWit\MapsUrls\UrlGenerator;
use CyrildeWit\MapsUrls\Actions\Directions;
use CyrildeWit\MapsUrls\Actions\Search;

$maps = new UrlGenerator(utmSource: 'my_app');

$searchUrl = $maps->generate(
    new Search(query: 'Eindhoven'),
    utmCampaign: 'search_request',
);
// https://www.google.com/maps/search/?api=1&query=Eindhoven&utm_source=my_app&utm_campaign=search_request

$directionsUrl = $maps->generate(
    new Directions(origin: 'Eindhoven', destination: 'Utrecht'),
    utmCampaign: 'directions_request',
);
// https://www.google.com/maps/dir/?api=1&origin=Eindhoven&destination=Utrecht&utm_source=my_app&utm_campaign=directions_request

use CyrildeWit\MapsUrls\Actions\Search;
use CyrildeWit\MapsUrls\Coordinates;

$action = new Search(query: new Coordinates(47.5951518, -122.3316393));

Coordinates::unchecked(48.857832, 542.295226);

use CyrildeWit\MapsUrls\Actions\Search;

$action = Search::fromArray([
    'query' => 'Eindhoven, Nederland',
]);

use CyrildeWit\MapsUrls\Actions\Search;
use CyrildeWit\MapsUrls\Coordinates;

$action = new Search(
    query: 'Eindhoven, Nederland',
    queryPlaceId: 'ChIJn8N5VRvZxkcRmLlkgWTSmvM',
);

$action = new Search(query: new Coordinates(47.5951518, -122.3316393));

use CyrildeWit\MapsUrls\MapsUrl;
use CyrildeWit\MapsUrls\Actions\Search;

$searchUrl = MapsUrl::for(Search::fromArray([
    'query' => 'Eindhoven, Nederland',
    'query_place_id' => 'ChIJn8N5VRvZxkcRmLlkgWTSmvM',
]));
// https://www.google.com/maps/search/?api=1&query=Eindhoven%2C+Nederland&query_place_id=ChIJn8N5VRvZxkcRmLlkgWTSmvM

use CyrildeWit\MapsUrls\Actions\Directions;
use CyrildeWit\MapsUrls\Enums\Avoid;
use CyrildeWit\MapsUrls\Enums\DirectionAction;
use CyrildeWit\MapsUrls\Enums\TravelMode;
use CyrildeWit\MapsUrls\Coordinates;

$action = new Directions(
    origin: 'Eindhoven, Nederland',
    originPlaceId: 'ChIJn8N5VRvZxkcRmLlkgWTSmvM',
    destination: 'Monnickendam, Nederland',
    destinationPlaceId: 'ChIJTZfQeLgFxkcRQhAYGf9HbrU',
    travelMode: TravelMode::Bicycling,
    directionAction: DirectionAction::Navigate,
    waypoints: ['Berlin,Germany', new Coordinates(48.8566, 2.3522)],
    avoid: [Avoid::Tolls, Avoid::Ferries],
);

TravelMode::Driving;
TravelMode::Walking;
TravelMode::Bicycling;
TravelMode::TwoWheeler;
TravelMode::Transit;

use CyrildeWit\MapsUrls\Actions\Directions;

$action = new Directions(
    waypoints: ['Berlin,Germany', 'Paris,France'],
    waypointPlaceIds: [
        'ChIJAVkDPzdOqEcRcDteW0YgIQQ',
        'ChIJD7fiBh9u5kcRYJSMaMOCCwQ',
    ],
);

Avoid::Ferries;
Avoid::Highways;
Avoid::Tolls;

use CyrildeWit\MapsUrls\MapsUrl;
use CyrildeWit\MapsUrls\Actions\Directions;

$directionsUrl = MapsUrl::for(Directions::fromArray([
    'origin' => 'Eindhoven, Nederland',
    'origin_place_id' => 'ChIJn8N5VRvZxkcRmLlkgWTSmvM',
    'destination' => 'Monnickendam, Nederland',
    'destination_place_id' => 'ChIJTZfQeLgFxkcRQhAYGf9HbrU',
    'travelmode' => 'driving',
    'dir_action' => 'navigate',
    'waypoints' => ['Berlin,Germany', [48.8566, 2.3522]],
    'avoid' => 'tolls',
]));
// https://www.google.com/maps/dir/?api=1&origin=Eindhoven%2C+Nederland&origin_place_id=ChIJn8N5VRvZxkcRmLlkgWTSmvM&destination=Monnickendam%2C+Nederland&destination_place_id=ChIJTZfQeLgFxkcRQhAYGf9HbrU&travelmode=driving&dir_action=navigate&waypoints=Berlin%2CGermany%7C48.8566%2C2.3522&avoid=tolls

use CyrildeWit\MapsUrls\Actions\DisplayMap;
use CyrildeWit\MapsUrls\Enums\BaseMap;
use CyrildeWit\MapsUrls\Enums\Layer;
use CyrildeWit\MapsUrls\Coordinates;

$action = new DisplayMap(
    center: new Coordinates(-33.8569, 151.2152),
    zoom: 10,
    baseMap: BaseMap::Satellite,
    layer: Layer::Transit,
);

BaseMap::Roadmap;
BaseMap::Satellite;
BaseMap::Terrain;

Layer::None;
Layer::Transit;
Layer::Traffic;
Layer::Bicycling;

use CyrildeWit\MapsUrls\MapsUrl;
use CyrildeWit\MapsUrls\Actions\DisplayMap;

$displayMapUrl = MapsUrl::for(DisplayMap::fromArray([
    'center' => [-33.8569, 151.2152],
    'zoom' => 10,
    'basemap' => 'satellite',
    'layer' => 'transit',
]));
// https://www.google.com/maps/@?api=1&map_action=map&center=-33.8569%2C151.2152&zoom=10&basemap=satellite&layer=transit

use CyrildeWit\MapsUrls\Actions\StreetViewPanorama;
use CyrildeWit\MapsUrls\Coordinates;

$action = new StreetViewPanorama(
    viewpoint: new Coordinates(48.857832, 2.295226),
    panoramaId: 'tu510ie_z4ptBZYo2BGEJg',
    heading: 120,
    pitch: 40,
    fov: 80,
);

use CyrildeWit\MapsUrls\MapsUrl;
use CyrildeWit\MapsUrls\Actions\StreetViewPanorama;

$panoramaUrl = MapsUrl::for(StreetViewPanorama::fromArray([
    'viewpoint' => [48.857832, 2.295226],
    'pano' => 'tu510ie_z4ptBZYo2BGEJg',
    'heading' => 120,
    'pitch' => 40,
    'fov' => 80,
]));
// https://www.google.com/maps/@?api=1&map_action=pano&viewpoint=48.857832%2C2.295226&pano=tu510ie_z4ptBZYo2BGEJg&heading=120&pitch=40&fov=80
sh
composer