c3t4r4/openlocationcode

Open Location Code (Plus Codes) implementation for PHP - Convert locations to and from short codes - Forked from google/open-location-code

Maintainers

Package info

github.com/c3t4r4/open-location-code

pkg:composer/c3t4r4/openlocationcode

Statistics

Installs: 155

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.1.1 2025-10-16 22:18 UTC

This package is auto-updated.

Last update: 2026-03-16 23:17:08 UTC


README

License PHP Version Tests Pest

A PHP implementation of Open Location Code (also known as Plus Codes).

Leia em Português

Open Location Code is a technology that provides a way to encode location into a form that is easier to use than latitude and longitude. The codes generated are called Plus Codes.

Requirements

  • PHP 8.2 or higher

Installation

Install via Composer:

composer require c3t4r4/openlocationcode

Basic Usage

Encode a location

use OpenLocationCode\OpenLocationCode;

// Encode with default precision (10 characters = ~13.5x13.5 meters)
$code = OpenLocationCode::encode(47.365590, 8.524997);
echo $code; // 8FVC9G8F+6X

// Encode with custom precision (11 characters = ~2.8x3.5 meters)
$code = OpenLocationCode::encode(47.365590, 8.524997, 11);
echo $code; // 8FVC9G8F+6XQ

Decode a code

use OpenLocationCode\OpenLocationCode;

$codeArea = OpenLocationCode::decode('8FVC9G8F+6X');

echo "Latitude Center: " . $codeArea->latitudeCenter . "\n";
echo "Longitude Center: " . $codeArea->longitudeCenter . "\n";
echo "Code Length: " . $codeArea->codeLength . "\n";

// Get center coordinates as array
[$lat, $lng] = $codeArea->getLatLng();

Shorten a code

use OpenLocationCode\OpenLocationCode;

$shortCode = OpenLocationCode::shorten('8FVC9G8F+6X', 47.5, 8.5);
echo $shortCode; // 9G8F+6X

Recover a full code

use OpenLocationCode\OpenLocationCode;

$fullCode = OpenLocationCode::recoverNearest('9G8F+6X', 47.4, 8.6);
echo $fullCode; // 8FVC9G8F+6X

Testing

This project uses Pest PHP for testing:

# Run all tests with Pest (recommended)
composer test

# Run with PHPUnit (legacy support)
composer test:phpunit

# Run specific test file
./vendor/bin/pest tests/BasicTest.pest.php

Test Status: ✅ 782/796 tests passing (98.2%)

  • All critical functionality tested and working
  • Round-trip encoding/decoding: 100% functional
  • Minor variations in 14 edge cases are mathematically acceptable

Documentation

For the official specification, visit the Open Location Code repository.

License

Copyright 2024 Google Inc.

Licensed under the Apache License, Version 2.0.