PHP code example of route4me / route4me-php

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

    

route4me / route4me-php example snippets


Route4Me\Route4Me::setApiKey('11111111111111111111111111111111');

$addresses = array();
$addresses[] = Route4Me\Address::fromArray(array(
    "lng"         => -85.757308,
    "lat"         => 38.251698,
    "is_depot"    => true,
    "time"        => 300,
    "sequence_no" => 0,
    "address"     => "455 S 4th St, Louisville, KY 40202"
));

$addresses[] = Route4Me\Address::fromArray(array(
    "lng"         => -85.793846,
    "lat"         => 38.141598,
    "is_depot"    => false,
    "time"        => 300,
    "sequence_no" => 1,
    "address"     => "1604 PARKRIDGE PKWY, Louisville, KY, 40214"
));

$addresses[] = Route4Me\Address::fromArray(array(
    "lng"         => -85.786514,
    "lat"         => 38.202496,
    "is_depot"    => false,
    "time"        => 300,
    "sequence_no" => 2,
    "address"     => "1407 MCCOY, Louisville, KY, 40215"
));

$addresses[] = Route4Me\Address::fromArray(array(
    "lng"         => -85.774864,
    "lat"         => 38.178844,
    "is_depot"    => false,
    "time"        => 300,
    "sequence_no" => 3,
    "address"     => "4805 BELLEVUE AVE, Louisville, KY, 40215"
));

$parameters = Route4Me\RouteParameters::fromArray(array(
    "algorithm_type"          => Route4Me\Enum\AlgorithmType::TSP,
    "distance_unit"           => Route4Me\Enum\DistanceUnit::MILES,
    "device_type"             => Route4Me\Enum\DeviceType::WEB,
    "optimize"                => Route4Me\Enum\OptimizationType::DISTANCE,
    "travel_mode"             => Route4Me\Enum\TravelMode::DRIVING,
    "route_max_duration"      => 86400,
    "store_route"             => true,
    "vehicle_capacity"        => 1,
    "vehicle_max_distance_mi" => 10000
));

$optimizationParams = new Route4Me\OptimizationProblemParams;
$optimizationParams->setAddresses($addresses);
$optimizationParams->setParameters($parameters);

$problem = Route4Me\OptimizationProblem::optimize($optimizationParams);

var_dump($problem);

Route4Me\Route4Me::setApiKey('11111111111111111111111111111111');

// Huge list of addresses
$json = json_decode(file_get_contents('./examples/addresses.json'), true);

$addresses = array();
foreach($json as $address) {
    $addresses[] = Address::fromArray($address);
}

$parameters = Route4Me\RouteParameters::fromArray(array(
    "algorithm_type"          => Route4Me\Enum\AlgorithmType::CVRP_TW_MD,
    "distance_unit"           => Route4Me\Enum\DistanceUnit::MILES,
    "device_type"             => Route4Me\Enum\DeviceType::WEB,
    "optimize"                => Route4Me\Enum\OptimizationType::DISTANCE,
    "travel_mode"             => Route4Me\Enum\TravelMode::DRIVING,
    "route_max_duration"      => 86400,
    "store_route"             => true,
    "vehicle_capacity"        => 50,
    "vehicle_max_distance_mi" => 10000,
    "parts"                   => 50
));

$optimizationParams = new Route4Me\OptimizationProblemParams;
$optimizationParams->setAddresses($addresses);
$optimizationParams->setParameters($parameters);

$problem = Route4Me\OptimizationProblem::optimize($optimizationParams);

var_dump($problem);