PHP code example of andydefer / laravel-locationiq

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

    

andydefer / laravel-locationiq example snippets


// routes/api.php



declare(strict_types=1);

use AndyDefer\PhpLocationIq\Enums\LocationIqBaseUrl;
use AndyDefer\PhpLocationIq\Enums\NominatimBaseUrl;
use AndyDefer\PhpLocationIq\LocationIqClient;
use AndyDefer\PhpLocationIq\NominatimClient;

return [
    'api_key' => env('LOCATIONIQ_API_KEY', ''),

    'base_url' => env('LOCATIONIQ_BASE_URL', LocationIqBaseUrl::US1->value),

    'locationiq_client_fqcn' => LocationIqClient::class,

    'nominatim' => [
        'base_url' => env('NOMINATIM_BASE_URL', NominatimBaseUrl::PUBLIC->value),
        'user_agent' => env('NOMINATIM_USER_AGENT', 'andydefer/laravel-locationiq'),
        'accept_language' => env('NOMINATIM_ACCEPT_LANGUAGE', 'fr'),
    ],

    'nominatim_client_fqcn' => NominatimClient::class,

    'directions' => [
        'profile' => env('LOCATIONIQ_DIRECTIONS_PROFILE', 'driving'),
        'accept_language' => env('LOCATIONIQ_ACCEPT_LANGUAGE'),
    ],

    'matrix' => [
        'profile' => env('LOCATIONIQ_MATRIX_PROFILE', 'driving'),
    ],
];

// config/locationiq.php
return [
    'api_key' => env('LOCATIONIQ_API_KEY', ''),
    'base_url' => LocationIqBaseUrl::EU1->value,

    'nominatim' => [
        'base_url' => NominatimBaseUrl::PUBLIC->value,
        'user_agent' => 'AfyaMedical/1.0 ([email protected])',
        'accept_language' => 'fr',
    ],

    'directions' => [
        'profile' => 'driving',
    ],

    'matrix' => [
        'profile' => 'driving',
    ],
];



declare(strict_types=1);

namespace AndyDefer\LaravelLocationIq\Contracts;

use AndyDefer\PhpLocationIq\Contracts\LocationIqClientInterface;
use AndyDefer\PhpLocationIq\Contracts\NominatimClientInterface;
use AndyDefer\PhpLocationIq\Enums\LocationIqBaseUrl;
use AndyDefer\PhpLocationIq\Enums\NominatimBaseUrl;

interface LocationIqConfigInterface
{
    public function getApiKey(): string;

    public function getBaseUrl(): LocationIqBaseUrl;

    /**
     * @return class-string<LocationIqClientInterface>
     */
    public function getLocationIqClientFqcn(): string;

    public function getNominatimBaseUrl(): NominatimBaseUrl;

    public function getUserAgent(): string;

    /**
     * @return class-string<NominatimClientInterface>
     */
    public function getNominatimClientFqcn(): string;

    public function getDefaultDirectionsProfile(): string;

    public function getDefaultAcceptLanguage(): ?string;
}

enum ErrorCode: string implements ErrorDescribable
{
    case INVALID_REQUEST = 'INVALID_REQUEST';
    case INVALID_KEY = 'INVALID_KEY';
    case ACCESS_RESTRICTED = 'ACCESS_RESTRICTED';
    case UNABLE_TO_GEOCODE = 'UNABLE_TO_GEOCODE';
    case RATE_LIMITED_DAY = 'RATE_LIMITED_DAY';
    case INVALID_OPTIONS = 'INVALID_OPTIONS';
    case INVALID_COORDINATES = 'INVALID_COORDINATES';
    case MISSING_API_KEY = 'MISSING_API_KEY';
    case NO_TABLE = 'NO_TABLE';
    case NOT_IMPLEMENTED = 'NOT_IMPLEMENTED';
    case UNKNOWN_ERROR = 'UNKNOWN_ERROR';

    public static function fromApiMessage(string $message): self;
}

// config/locationiq.php
'locationiq_client_fqcn' => \App\Geo\AfyaLocationIqClient::class,

// config/locationiq.php
'nominatim_client_fqcn' => \App\Geo\AfyaNominatimClient::class,

$this->app->bind(
    LocationIqClientInterface::class,
    AfyaLocationIqClient::class,
);

$this->app->bind(
    NominatimClientInterface::class,
    AfyaNominatimClient::class,
);

public function test_it_returns_day_balance_on_success(): void
{
    // Arrange: the API will answer 200 with a positive balance
    $this->locationIqClient->addBalanceSuccessResponse(30000);

    // Act: hit the route
    $response = $this->postJson('/api/locationiq-balance');

    // Assert: 200 and balance.day exposed
    $response->assertOk();
    $response->assertJsonPath('balance.day', 30000);
}

$this->app->instance(LocationIqConfigInterface::class, new RestrictedLocationIqConfig);
bash
php artisan vendor:publish --tag=laravel-locationiq-config
php artisan vendor:publish --tag=laravel-locationiq-routes