Repository files navigation

geocod.io PHP library Latest VersionTotal Downloads

Library for performing forward and reverse address geocoding for addresses or coordinates in the US, Canada, Mexico and the UK.

Installation

You can install the package via composer:

composer require geocodio/geocodio-library-php

Using Laravel? Great! There's an optional Laravel service provider, for easy integration into your app.

Usage

Don't have an API key yet? Sign up at https://dash.geocod.io to get an API key. The first 2,500 lookups per day are free.

Single geocoding

Using the Laravel integration? Check out Laravel-specific usage examples below.

$geocoder = newGeocodio\Geocodio();
$geocoder->setApiKey('YOUR_API_KEY');
// $geocoder->setHostname('api-hipaa.geocod.io'); // optionally overwrite the API hostname$response = $geocoder->geocode('1109 N Highland St, Arlington, VA');
dump($response);
/*array:1 [ "results" => array:1 [ 0 => array:6 [ "address_components" => array:10 [ "number" => "1109" "predirectional" => "N" "street" => "Highland" "suffix" => "St" "formatted_street" => "N Highland St" "city" => "Arlington" "county" => "Arlington County" "state_province" => "VA" "postal_code" => "22201" "country" => "US" ] "formatted_address" => "1109 N Highland St, Arlington, VA 22201" "location" => array:2 [ "lat" => 38.886672 "lng" => -77.094735 ] "accuracy" => 1 "accuracy_type" => "rooftop" "source" => "Arlington" ] ]]*/$response = $geocoder->reverse('38.9002898,-76.9990361');
$response = $geocoder->reverse([38.9002898, -76.9990361]);

Note: You can read more about accuracy scores, accuracy types, input formats and more at https://www.geocod.io/docs/

Batch geocoding

To batch geocode, simply pass an array of addresses or coordinates instead of a single string

$response = $geocoder->geocode([
'1109 N Highland St, Arlington VA',
'525 University Ave, Toronto, ON, Canada',
'10 Downing St, London, United Kingdom',
'4410 S Highway 17 92, Casselberry FL',
'15000 NE 24th Street, Redmond WA',
'17015 Walnut Grove Drive, Morgan Hill CA'
]);
$response = $geocoder->reverse([
'35.9746000,-77.9658000',
'32.8793700,-96.6303900',
'33.8337100,-117.8362320',
'35.4171240,-80.6784760'
]);
// Optionally supply a custom key that will be returned along with results$response = $geocoder->geocode([
'MyId1' => '1109 N Highland St, Arlington VA',
'MyId2' => '525 University Ave, Toronto, ON, Canada',
'MyId3' => '10 Downing St, London, United Kingdom',
'MyId4' => '4410 S Highway 17 92, Casselberry FL',
'MyId5' => '15000 NE 24th Street, Redmond WA',
'MyId6' => '17015 Walnut Grove Drive, Morgan Hill CA'
]);

Field appends

Geocodio allows you to append additional data points such as congressional districts, census codes, timezone, ACS survey results, UK constituencies and wards, and much much more.

To request additional fields, simply supply them as an array as the second parameter

$response = $geocoder->geocode(
[
'1109 N Highland St, Arlington VA',
'525 University Ave, Toronto, ON, Canada'
],
[
'cd',
'timezone'
]
);
$response = $geocoder->reverse('38.9002898,-76.9990361', ['census2010']);
// United Kingdom addresses support UK-specific appends such as Westminster and// devolved parliament constituencies, and local authority wards$response = $geocoder->geocode(
'10 Downing St, London, United Kingdom',
['uk-westminster', 'uk-local']
);

Address components

For forward geocoding requests it is possible to supply individual address components instead of a full address string. This works for both single and batch geocoding requests.

$response = $geocoder->geocode([
'street' => '1109 N Highland St',
'city' => 'Arlington',
'state_province' => 'VA',
'postal_code' => '22201'
]);
$response = $geocoder->geocode([
[
'street' => '1109 N Highland St',
'city' => 'Arlington',
'state_province' => 'VA'
],
[
'street' => '525 University Ave',
'city' => 'Toronto',
'state_province' => 'ON',
'country' => 'Canada',
],
[
'street' => '10 Downing St',
'city' => 'London',
'postal_code' => 'SW1A 2AA',
'country' => 'United Kingdom',
],
]);

Limit results

Optionally limit the number of maximum geocoding results by using the third parameter on geocode(...) or reverse(...)

$response = $geocoder->geocode('1109 N Highland St, Arlington, VA', [], 1); // Only get the first result$response = $geocoder->reverse('38.9002898,-76.9990361', ['timezone'], 5); // Return up to 5 geocoding results

Uploading Lists

The lists API lets you upload and process spreadsheet with addresses or coordinates. Similar to the spreadsheet feature in the dashboard, the spreadsheet will be processed as a job on Geocodio's infrastructure and can be downloaded at a later time. While a spreadsheet is being processed it is possible to query the status and progress.

Important

Data for spreadsheets processed through the lists API are automatically deleted 72 hours after they have finished processing. In addition to a 1GB file size limit, we recommend a maximum of 10M lookups per list batch. Larger batches should be split up into multiple list jobs.

See the API docs for geocoding lists for additional details.

Upload list from a file

Creates a new spreadsheet list job and starts processing the list in the background. The response returns a list id that can be used to retrieve the job progress as well as download the processed list when it has completed.

$response = $geocoder->uploadList(
file: 'path/to/file.csv',
direction: GeocodeDirection::Forward,
format: '{{B}} {{C}} {{D}} {{E}}',
callbackWebhook: 'https://example.com/callbacks/list-upload',
);
/*array:2 [ "id" => 11953719 "file" => array:3 [ "headers" => array:5 [ 0 => "Name" 1 => "Address" 2 => "City" 3 => "State" 4 => "Zip" ] "estimated_rows_count" => 4 "filename" => "simple.csv" ]]*/

Upload list of inline data

$csvData = <<<'CSV'name,street,city,state,zip"Peregrine Espresso","660 Pennsylvania Ave SE",Washington,DC,20003"Lot 38 Espresso Bar","1001 2nd St SE",Washington,DC,20003
CSV;
$geocodio->uploadInlineList(
$csvData,
'coffee-shops.csv',
GeocodeDirection::Forward,
'{{B}} {{C}} {{D}} {{E}}'
);

List status

View the metadata and status for a single uploaded list.

$geocoder->listStatus(11950669);
/*array:6 [ "id" => 11953719 "fields" => [] "file" => array:2 [ "estimated_rows_count" => 4 "filename" => "simple.csv" ] "status" => array:5 [ "state" => "COMPLETED" "progress" => 100 "message" => "Completed" "time_left_description" => null "time_left_seconds" => null ] "download_url" => "https://api.geocod.io/v2/lists/11953719/download" "expires_at" => "2025-08-22T20:36:10.000000Z"]*/

Download a list

Download a fully geocoded list, the returned format will always be a UTF-8 encoded, comma-separated csv file.

$geocoder->downloadList(11950669, 'path/to/file.csv');

Fetch all uploaded lists

Show all lists that have been created. The endpoint is paginated, showing 15 lists at a time, ordered by recency.

$geocoder->lists();
/*array:9 [ "current_page" => 1 "data" => array:1 [ 0 => array:6 [ "id" => 11953719 "fields" => [] "file" => array:2 [ "estimated_rows_count" => 4 "filename" => "simple.csv" ] "status" => array:5 [ "state" => "COMPLETED" "progress" => 100 "message" => "Completed" "time_left_description" => null "time_left_seconds" => null ] "download_url" => "https://api.geocod.io/v2/lists/11953719/download" "expires_at" => "2025-08-22T20:36:10.000000Z" ] "first_page_url" => "https://api.geocod.io/v2/lists?page=1" "from" => 1 "next_page_url" => null "path" => "https://api.geocod.io/v2/lists" "per_page" => 15 "prev_page_url" => null "to" => 3]*/

Delete uploaded list

Delete a previously uploaded list and its underlying spreadsheet data permanently. This can also be used to cancel and delete a spreadsheet that is currently processing.

Geocodio Unlimited customers can cancel a spreadsheet at any time. Pay as You Go customers can only cancel a spreadsheet if it was just recently started.

The spreadsheet data will always be deleted automatically after 72 hours if it is not deleted manually first.

$geocoder->deleteList(11950669);

Distance calculations

Calculate distances from a single origin to multiple destinations, or compute full distance matrices.

Coordinate format with custom IDs

You can add custom identifiers to coordinates using the lat,lng,id format. The ID will be returned in the response, making it easy to match results back to your data:

// String format with ID'37.7749,-122.4194,warehouse_1'// Array format with ID
[37.7749, -122.4194, 'warehouse_1']
// The ID is returned in the response:// [// 'query' => '37.7749,-122.4194,warehouse_1',// 'location' => [37.7749, -122.4194],// 'id' => 'warehouse_1',// 'distance_miles' => 3.2,// 'distance_km' => 5.1// ]

Distance mode and units

The SDK provides enums for type-safe distance configuration:

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Available modes
DistanceMode::Straightline // Default - great-circle (as the crow flies)
DistanceMode::Driving // Road network routing with duration// Available units
DistanceUnits::Miles // Default
DistanceUnits::Km
// Sorting options
DistanceOrderBy::Distance // Default
DistanceOrderBy::Duration
DistanceSortOrder::Asc // Default
DistanceSortOrder::Desc

Note: The default mode is straightline (great-circle distance). Use DistanceMode::Driving if you need road network routing with duration estimates.

Add distance to geocoding requests

You can add distance calculations to existing geocode or reverse geocode requests. Each geocoded result will include a destinations array with distances to each destination.

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Geocode an address and calculate distances to store locations$response = $geocoder->geocode(
'1600 Pennsylvania Ave NW, Washington DC',
destinations: [
'38.9072,-77.0369,store_dc',
'39.2904,-76.6122,store_baltimore',
'39.9526,-75.1652,store_philly'
],
distanceMode: DistanceMode::Driving,
distanceUnits: DistanceUnits::Miles
);
/*Response includes destinations for each geocoded result:[ 'results' => [ [ 'formatted_address' => '1600 Pennsylvania Ave NW, Washington, DC 20500', 'location' => ['lat' => 38.8977, 'lng' => -77.0365], 'destinations' => [ [ 'query' => '38.9072,-77.0369,store_dc', 'location' => [38.9072, -77.0369], 'id' => 'store_dc', 'distance_miles' => 0.8, 'distance_km' => 1.3, 'duration_seconds' => 180 ], [ 'query' => '39.2904,-76.6122,store_baltimore', 'location' => [39.2904, -76.6122], 'id' => 'store_baltimore', 'distance_miles' => 38.2, 'distance_km' => 61.5, 'duration_seconds' => 2820 ], // ... ] ] ]]*/// Reverse geocode with distances$response = $geocoder->reverse(
query: '38.8977,-77.0365',
destinations: [
'38.9072,-77.0369,capitol',
'38.8895,-77.0353,monument'
],
distanceMode: DistanceMode::Straightline
);
// With filtering - find nearest 3 stores within 50 miles$response = $geocoder->geocode(
'1600 Pennsylvania Ave NW, Washington DC',
destinations: [
'38.9072,-77.0369,store_1',
'39.2904,-76.6122,store_2',
'39.9526,-75.1652,store_3',
'40.7128,-74.0060,store_4'
],
distanceMode: DistanceMode::Driving,
distanceMaxResults: 3,
distanceMaxDistance: 50.0,
distanceOrderBy: DistanceOrderBy::Distance,
distanceSortOrder: DistanceSortOrder::Asc
);

Single origin to multiple destinations

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Calculate distances from one origin to multiple destinations$response = $geocoder->distance(
'37.7749,-122.4194,headquarters', // Origin with ID
[
'37.7849,-122.4094,customer_a',
'37.7949,-122.3994,customer_b',
'37.8049,-122.4294,customer_c'
]
);
/*Response:[ 'origin' => [ 'query' => '37.7749,-122.4194,headquarters', 'location' => [37.7749, -122.4194], 'id' => 'headquarters' ], 'destinations' => [ [ 'query' => '37.7849,-122.4094,customer_a', 'location' => [37.7849, -122.4094], 'id' => 'customer_a', 'distance_miles' => 0.9, 'distance_km' => 1.4 ], // ... ]]*/// Use driving mode for road network routing (includes duration)$response = $geocoder->distance(
'37.7749,-122.4194',
['37.7849,-122.4094'],
mode: DistanceMode::Driving
);
// With all filtering and sorting options$response = $geocoder->distance(
origin: '37.7749,-122.4194,warehouse',
destinations: [
'37.7849,-122.4094,store_1',
'37.7949,-122.3994,store_2',
'37.8049,-122.4294,store_3'
],
mode: DistanceMode::Driving,
units: DistanceUnits::Km,
maxResults: 2,
maxDistance: 10.0,
orderBy: DistanceOrderBy::Distance,
sortOrder: DistanceSortOrder::Asc
);
// Array format for coordinates (with or without ID)$response = $geocoder->distance(
[37.7749, -122.4194], // Without ID
[[37.7849, -122.4094, 'dest_1']] // With ID as third element
);

Distance matrix (multiple origins × destinations)

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Calculate full distance matrix with custom IDs$response = $geocoder->distanceMatrix(
origins: [
'37.7749,-122.4194,warehouse_sf',
'37.8049,-122.4294,warehouse_oak'
],
destinations: [
'37.7849,-122.4094,customer_1',
'37.7949,-122.3994,customer_2'
]
);
/*Response structure:[ 'results' => [ [ 'origin' => [ 'query' => '37.7749,-122.4194,warehouse_sf', 'location' => [37.7749, -122.4194], 'id' => 'warehouse_sf' ], 'destinations' => [ [ 'query' => '37.7849,-122.4094,customer_1', 'location' => [37.7849, -122.4094], 'id' => 'customer_1', 'distance_miles' => 0.9, 'distance_km' => 1.4 ], // ... ] ], [ 'origin' => [..., 'id' => 'warehouse_oak'], 'destinations' => [...] ] ]]*/// With driving mode and kilometers$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: ['37.7849,-122.4094'],
mode: DistanceMode::Driving,
units: DistanceUnits::Km
);

Nearest mode (find closest destinations)

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Find up to 2 nearest destinations from each origin$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: ['37.7849,-122.4094', '37.7949,-122.3994', '37.8049,-122.4294'],
maxResults: 2
);
// Filter by maximum distance (in miles or km depending on units)$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: [...],
maxDistance: 2.0
);
// Filter by minimum and maximum distance$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: [...],
minDistance: 1.0,
maxDistance: 10.0
);
// Filter by duration (seconds, driving mode only)$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: [...],
mode: DistanceMode::Driving,
maxDuration: 300, // 5 minutes
minDuration: 60// 1 minute minimum
);
// Sort by duration descending$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: [...],
mode: DistanceMode::Driving,
maxResults: 5,
orderBy: DistanceOrderBy::Duration,
sortOrder: DistanceSortOrder::Desc
);

Async Distance Matrix Jobs

For large distance matrix calculations, use async jobs that process in the background.

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
// Create a new distance matrix job$job = $geocoder->createDistanceMatrixJob(
name: 'My Distance Calculation',
origins: ['37.7749,-122.4194', '37.8049,-122.4294'],
destinations: ['37.7849,-122.4094', '37.7949,-122.3994'],
mode: DistanceMode::Driving,
units: DistanceUnits::Miles,
callbackUrl: 'https://example.com/webhook'// Optional
);
// Or use list IDs from previously uploaded lists$job = $geocoder->createDistanceMatrixJob(
name: 'Distance from List',
origins: 12345, // List ID
destinations: 67890, // List ID
mode: DistanceMode::Straightline
);
echo$job['id']; // Job identifier// Check job status$status = $geocoder->distanceMatrixJobStatus($job['id']);
/*[ 'id' => 'abc123', 'name' => 'My Distance Calculation', 'status' => [ 'state' => 'COMPLETED', // or 'PROCESSING', 'FAILED' 'progress' => 100, 'message' => 'Completed' ], 'download_url' => 'https://api.geocod.io/v2/distance-matrix/abc123/download', 'expires_at' => '2025-01-15T12:00:00.000000Z']*/// List all jobs$jobs = $geocoder->distanceMatrixJobs();
$jobs = $geocoder->distanceMatrixJobs(page: 2); // Paginated// Get job results as parsed array (same format as distance POST)$results = $geocoder->getDistanceMatrixJobResults($job['id']);
/*[ 'results' => [ [ 'origin' => ['query' => '...', 'location' => [...], 'id' => '...'], 'destinations' => [ ['query' => '...', 'location' => [...], 'id' => '...', 'distance_miles' => 1.2, ...] ] ], // ... ]]*/// Or download to file for very large results$geocoder->downloadDistanceMatrixJob($job['id'], '/path/to/results.json');
// Delete a job$geocoder->deleteDistanceMatrixJob($job['id']);

Note: Distance lookups are billed separately, please refer to https://www.geocod.io/pricing/ for more information.

Usage with Laravel

This library works well without Laravel, but if you happen to be using Laravel you can enjoy a few Laravel-specific features.

The package will be auto-discovered by newer Laravel versions, so the only thing left to do is to publish the config file

php artisan vendor:publish --provider="Geocodio\GeocodioServiceProvider"

You can now go ahead and edit your config file at config/geocodio.php.

You will now be able to use the Geocodio facade, or dependency inject the fully-configured Geocodio class.

// Using facadeuseGeocodio;
$response = Geocodio::geocode('1109 N Highland St, Arlington, VA');
// Using dependency injectionuseGeocodio\Geocodio;
class SomeController {
publicfunction__construct(Geocodio$geocoder) {
$response = $geocoder->geocode('1109 N Highland St, Arlington, VA');
}
}

Testing

$ composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Security

If you discover any security related issues, please email security@geocod.io instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.

About

geocod.io PHP library

Resources

Stars

106 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Repository files navigation

geocod.io PHP library Latest VersionTotal Downloads

Library for performing forward and reverse address geocoding for addresses or coordinates in the US, Canada, Mexico and the UK.

Installation

You can install the package via composer:

composer require geocodio/geocodio-library-php

Using Laravel? Great! There's an optional Laravel service provider, for easy integration into your app.

Usage

Don't have an API key yet? Sign up at https://dash.geocod.io to get an API key. The first 2,500 lookups per day are free.

Single geocoding

Using the Laravel integration? Check out Laravel-specific usage examples below.

$geocoder = newGeocodio\Geocodio();
$geocoder->setApiKey('YOUR_API_KEY');
// $geocoder->setHostname('api-hipaa.geocod.io'); // optionally overwrite the API hostname$response = $geocoder->geocode('1109 N Highland St, Arlington, VA');
dump($response);
/*array:1 [ "results" => array:1 [ 0 => array:6 [ "address_components" => array:10 [ "number" => "1109" "predirectional" => "N" "street" => "Highland" "suffix" => "St" "formatted_street" => "N Highland St" "city" => "Arlington" "county" => "Arlington County" "state_province" => "VA" "postal_code" => "22201" "country" => "US" ] "formatted_address" => "1109 N Highland St, Arlington, VA 22201" "location" => array:2 [ "lat" => 38.886672 "lng" => -77.094735 ] "accuracy" => 1 "accuracy_type" => "rooftop" "source" => "Arlington" ] ]]*/$response = $geocoder->reverse('38.9002898,-76.9990361');
$response = $geocoder->reverse([38.9002898, -76.9990361]);

Note: You can read more about accuracy scores, accuracy types, input formats and more at https://www.geocod.io/docs/

Batch geocoding

To batch geocode, simply pass an array of addresses or coordinates instead of a single string

$response = $geocoder->geocode([
'1109 N Highland St, Arlington VA',
'525 University Ave, Toronto, ON, Canada',
'10 Downing St, London, United Kingdom',
'4410 S Highway 17 92, Casselberry FL',
'15000 NE 24th Street, Redmond WA',
'17015 Walnut Grove Drive, Morgan Hill CA'
]);
$response = $geocoder->reverse([
'35.9746000,-77.9658000',
'32.8793700,-96.6303900',
'33.8337100,-117.8362320',
'35.4171240,-80.6784760'
]);
// Optionally supply a custom key that will be returned along with results$response = $geocoder->geocode([
'MyId1' => '1109 N Highland St, Arlington VA',
'MyId2' => '525 University Ave, Toronto, ON, Canada',
'MyId3' => '10 Downing St, London, United Kingdom',
'MyId4' => '4410 S Highway 17 92, Casselberry FL',
'MyId5' => '15000 NE 24th Street, Redmond WA',
'MyId6' => '17015 Walnut Grove Drive, Morgan Hill CA'
]);

Field appends

Geocodio allows you to append additional data points such as congressional districts, census codes, timezone, ACS survey results, UK constituencies and wards, and much much more.

To request additional fields, simply supply them as an array as the second parameter

$response = $geocoder->geocode(
[
'1109 N Highland St, Arlington VA',
'525 University Ave, Toronto, ON, Canada'
],
[
'cd',
'timezone'
]
);
$response = $geocoder->reverse('38.9002898,-76.9990361', ['census2010']);
// United Kingdom addresses support UK-specific appends such as Westminster and// devolved parliament constituencies, and local authority wards$response = $geocoder->geocode(
'10 Downing St, London, United Kingdom',
['uk-westminster', 'uk-local']
);

Address components

For forward geocoding requests it is possible to supply individual address components instead of a full address string. This works for both single and batch geocoding requests.

$response = $geocoder->geocode([
'street' => '1109 N Highland St',
'city' => 'Arlington',
'state_province' => 'VA',
'postal_code' => '22201'
]);
$response = $geocoder->geocode([
[
'street' => '1109 N Highland St',
'city' => 'Arlington',
'state_province' => 'VA'
],
[
'street' => '525 University Ave',
'city' => 'Toronto',
'state_province' => 'ON',
'country' => 'Canada',
],
[
'street' => '10 Downing St',
'city' => 'London',
'postal_code' => 'SW1A 2AA',
'country' => 'United Kingdom',
],
]);

Limit results

Optionally limit the number of maximum geocoding results by using the third parameter on geocode(...) or reverse(...)

$response = $geocoder->geocode('1109 N Highland St, Arlington, VA', [], 1); // Only get the first result$response = $geocoder->reverse('38.9002898,-76.9990361', ['timezone'], 5); // Return up to 5 geocoding results

Uploading Lists

The lists API lets you upload and process spreadsheet with addresses or coordinates. Similar to the spreadsheet feature in the dashboard, the spreadsheet will be processed as a job on Geocodio's infrastructure and can be downloaded at a later time. While a spreadsheet is being processed it is possible to query the status and progress.

Important

Data for spreadsheets processed through the lists API are automatically deleted 72 hours after they have finished processing. In addition to a 1GB file size limit, we recommend a maximum of 10M lookups per list batch. Larger batches should be split up into multiple list jobs.

See the API docs for geocoding lists for additional details.

Upload list from a file

Creates a new spreadsheet list job and starts processing the list in the background. The response returns a list id that can be used to retrieve the job progress as well as download the processed list when it has completed.

$response = $geocoder->uploadList(
file: 'path/to/file.csv',
direction: GeocodeDirection::Forward,
format: '{{B}} {{C}} {{D}} {{E}}',
callbackWebhook: 'https://example.com/callbacks/list-upload',
);
/*array:2 [ "id" => 11953719 "file" => array:3 [ "headers" => array:5 [ 0 => "Name" 1 => "Address" 2 => "City" 3 => "State" 4 => "Zip" ] "estimated_rows_count" => 4 "filename" => "simple.csv" ]]*/

Upload list of inline data

$csvData = <<<'CSV'name,street,city,state,zip"Peregrine Espresso","660 Pennsylvania Ave SE",Washington,DC,20003"Lot 38 Espresso Bar","1001 2nd St SE",Washington,DC,20003
CSV;
$geocodio->uploadInlineList(
$csvData,
'coffee-shops.csv',
GeocodeDirection::Forward,
'{{B}} {{C}} {{D}} {{E}}'
);

List status

View the metadata and status for a single uploaded list.

$geocoder->listStatus(11950669);
/*array:6 [ "id" => 11953719 "fields" => [] "file" => array:2 [ "estimated_rows_count" => 4 "filename" => "simple.csv" ] "status" => array:5 [ "state" => "COMPLETED" "progress" => 100 "message" => "Completed" "time_left_description" => null "time_left_seconds" => null ] "download_url" => "https://api.geocod.io/v2/lists/11953719/download" "expires_at" => "2025-08-22T20:36:10.000000Z"]*/

Download a list

Download a fully geocoded list, the returned format will always be a UTF-8 encoded, comma-separated csv file.

$geocoder->downloadList(11950669, 'path/to/file.csv');

Fetch all uploaded lists

Show all lists that have been created. The endpoint is paginated, showing 15 lists at a time, ordered by recency.

$geocoder->lists();
/*array:9 [ "current_page" => 1 "data" => array:1 [ 0 => array:6 [ "id" => 11953719 "fields" => [] "file" => array:2 [ "estimated_rows_count" => 4 "filename" => "simple.csv" ] "status" => array:5 [ "state" => "COMPLETED" "progress" => 100 "message" => "Completed" "time_left_description" => null "time_left_seconds" => null ] "download_url" => "https://api.geocod.io/v2/lists/11953719/download" "expires_at" => "2025-08-22T20:36:10.000000Z" ] "first_page_url" => "https://api.geocod.io/v2/lists?page=1" "from" => 1 "next_page_url" => null "path" => "https://api.geocod.io/v2/lists" "per_page" => 15 "prev_page_url" => null "to" => 3]*/

Delete uploaded list

Delete a previously uploaded list and its underlying spreadsheet data permanently. This can also be used to cancel and delete a spreadsheet that is currently processing.

Geocodio Unlimited customers can cancel a spreadsheet at any time. Pay as You Go customers can only cancel a spreadsheet if it was just recently started.

The spreadsheet data will always be deleted automatically after 72 hours if it is not deleted manually first.

$geocoder->deleteList(11950669);

Distance calculations

Calculate distances from a single origin to multiple destinations, or compute full distance matrices.

Coordinate format with custom IDs

You can add custom identifiers to coordinates using the lat,lng,id format. The ID will be returned in the response, making it easy to match results back to your data:

// String format with ID'37.7749,-122.4194,warehouse_1'// Array format with ID
[37.7749, -122.4194, 'warehouse_1']
// The ID is returned in the response:// [// 'query' => '37.7749,-122.4194,warehouse_1',// 'location' => [37.7749, -122.4194],// 'id' => 'warehouse_1',// 'distance_miles' => 3.2,// 'distance_km' => 5.1// ]

Distance mode and units

The SDK provides enums for type-safe distance configuration:

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Available modes
DistanceMode::Straightline // Default - great-circle (as the crow flies)
DistanceMode::Driving // Road network routing with duration// Available units
DistanceUnits::Miles // Default
DistanceUnits::Km
// Sorting options
DistanceOrderBy::Distance // Default
DistanceOrderBy::Duration
DistanceSortOrder::Asc // Default
DistanceSortOrder::Desc

Note: The default mode is straightline (great-circle distance). Use DistanceMode::Driving if you need road network routing with duration estimates.

Add distance to geocoding requests

You can add distance calculations to existing geocode or reverse geocode requests. Each geocoded result will include a destinations array with distances to each destination.

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Geocode an address and calculate distances to store locations$response = $geocoder->geocode(
'1600 Pennsylvania Ave NW, Washington DC',
destinations: [
'38.9072,-77.0369,store_dc',
'39.2904,-76.6122,store_baltimore',
'39.9526,-75.1652,store_philly'
],
distanceMode: DistanceMode::Driving,
distanceUnits: DistanceUnits::Miles
);
/*Response includes destinations for each geocoded result:[ 'results' => [ [ 'formatted_address' => '1600 Pennsylvania Ave NW, Washington, DC 20500', 'location' => ['lat' => 38.8977, 'lng' => -77.0365], 'destinations' => [ [ 'query' => '38.9072,-77.0369,store_dc', 'location' => [38.9072, -77.0369], 'id' => 'store_dc', 'distance_miles' => 0.8, 'distance_km' => 1.3, 'duration_seconds' => 180 ], [ 'query' => '39.2904,-76.6122,store_baltimore', 'location' => [39.2904, -76.6122], 'id' => 'store_baltimore', 'distance_miles' => 38.2, 'distance_km' => 61.5, 'duration_seconds' => 2820 ], // ... ] ] ]]*/// Reverse geocode with distances$response = $geocoder->reverse(
query: '38.8977,-77.0365',
destinations: [
'38.9072,-77.0369,capitol',
'38.8895,-77.0353,monument'
],
distanceMode: DistanceMode::Straightline
);
// With filtering - find nearest 3 stores within 50 miles$response = $geocoder->geocode(
'1600 Pennsylvania Ave NW, Washington DC',
destinations: [
'38.9072,-77.0369,store_1',
'39.2904,-76.6122,store_2',
'39.9526,-75.1652,store_3',
'40.7128,-74.0060,store_4'
],
distanceMode: DistanceMode::Driving,
distanceMaxResults: 3,
distanceMaxDistance: 50.0,
distanceOrderBy: DistanceOrderBy::Distance,
distanceSortOrder: DistanceSortOrder::Asc
);

Single origin to multiple destinations

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Calculate distances from one origin to multiple destinations$response = $geocoder->distance(
'37.7749,-122.4194,headquarters', // Origin with ID
[
'37.7849,-122.4094,customer_a',
'37.7949,-122.3994,customer_b',
'37.8049,-122.4294,customer_c'
]
);
/*Response:[ 'origin' => [ 'query' => '37.7749,-122.4194,headquarters', 'location' => [37.7749, -122.4194], 'id' => 'headquarters' ], 'destinations' => [ [ 'query' => '37.7849,-122.4094,customer_a', 'location' => [37.7849, -122.4094], 'id' => 'customer_a', 'distance_miles' => 0.9, 'distance_km' => 1.4 ], // ... ]]*/// Use driving mode for road network routing (includes duration)$response = $geocoder->distance(
'37.7749,-122.4194',
['37.7849,-122.4094'],
mode: DistanceMode::Driving
);
// With all filtering and sorting options$response = $geocoder->distance(
origin: '37.7749,-122.4194,warehouse',
destinations: [
'37.7849,-122.4094,store_1',
'37.7949,-122.3994,store_2',
'37.8049,-122.4294,store_3'
],
mode: DistanceMode::Driving,
units: DistanceUnits::Km,
maxResults: 2,
maxDistance: 10.0,
orderBy: DistanceOrderBy::Distance,
sortOrder: DistanceSortOrder::Asc
);
// Array format for coordinates (with or without ID)$response = $geocoder->distance(
[37.7749, -122.4194], // Without ID
[[37.7849, -122.4094, 'dest_1']] // With ID as third element
);

Distance matrix (multiple origins × destinations)

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Calculate full distance matrix with custom IDs$response = $geocoder->distanceMatrix(
origins: [
'37.7749,-122.4194,warehouse_sf',
'37.8049,-122.4294,warehouse_oak'
],
destinations: [
'37.7849,-122.4094,customer_1',
'37.7949,-122.3994,customer_2'
]
);
/*Response structure:[ 'results' => [ [ 'origin' => [ 'query' => '37.7749,-122.4194,warehouse_sf', 'location' => [37.7749, -122.4194], 'id' => 'warehouse_sf' ], 'destinations' => [ [ 'query' => '37.7849,-122.4094,customer_1', 'location' => [37.7849, -122.4094], 'id' => 'customer_1', 'distance_miles' => 0.9, 'distance_km' => 1.4 ], // ... ] ], [ 'origin' => [..., 'id' => 'warehouse_oak'], 'destinations' => [...] ] ]]*/// With driving mode and kilometers$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: ['37.7849,-122.4094'],
mode: DistanceMode::Driving,
units: DistanceUnits::Km
);

Nearest mode (find closest destinations)

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Find up to 2 nearest destinations from each origin$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: ['37.7849,-122.4094', '37.7949,-122.3994', '37.8049,-122.4294'],
maxResults: 2
);
// Filter by maximum distance (in miles or km depending on units)$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: [...],
maxDistance: 2.0
);
// Filter by minimum and maximum distance$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: [...],
minDistance: 1.0,
maxDistance: 10.0
);
// Filter by duration (seconds, driving mode only)$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: [...],
mode: DistanceMode::Driving,
maxDuration: 300, // 5 minutes
minDuration: 60// 1 minute minimum
);
// Sort by duration descending$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: [...],
mode: DistanceMode::Driving,
maxResults: 5,
orderBy: DistanceOrderBy::Duration,
sortOrder: DistanceSortOrder::Desc
);

Async Distance Matrix Jobs

For large distance matrix calculations, use async jobs that process in the background.

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
// Create a new distance matrix job$job = $geocoder->createDistanceMatrixJob(
name: 'My Distance Calculation',
origins: ['37.7749,-122.4194', '37.8049,-122.4294'],
destinations: ['37.7849,-122.4094', '37.7949,-122.3994'],
mode: DistanceMode::Driving,
units: DistanceUnits::Miles,
callbackUrl: 'https://example.com/webhook'// Optional
);
// Or use list IDs from previously uploaded lists$job = $geocoder->createDistanceMatrixJob(
name: 'Distance from List',
origins: 12345, // List ID
destinations: 67890, // List ID
mode: DistanceMode::Straightline
);
echo$job['id']; // Job identifier// Check job status$status = $geocoder->distanceMatrixJobStatus($job['id']);
/*[ 'id' => 'abc123', 'name' => 'My Distance Calculation', 'status' => [ 'state' => 'COMPLETED', // or 'PROCESSING', 'FAILED' 'progress' => 100, 'message' => 'Completed' ], 'download_url' => 'https://api.geocod.io/v2/distance-matrix/abc123/download', 'expires_at' => '2025-01-15T12:00:00.000000Z']*/// List all jobs$jobs = $geocoder->distanceMatrixJobs();
$jobs = $geocoder->distanceMatrixJobs(page: 2); // Paginated// Get job results as parsed array (same format as distance POST)$results = $geocoder->getDistanceMatrixJobResults($job['id']);
/*[ 'results' => [ [ 'origin' => ['query' => '...', 'location' => [...], 'id' => '...'], 'destinations' => [ ['query' => '...', 'location' => [...], 'id' => '...', 'distance_miles' => 1.2, ...] ] ], // ... ]]*/// Or download to file for very large results$geocoder->downloadDistanceMatrixJob($job['id'], '/path/to/results.json');
// Delete a job$geocoder->deleteDistanceMatrixJob($job['id']);

Note: Distance lookups are billed separately, please refer to https://www.geocod.io/pricing/ for more information.

Usage with Laravel

This library works well without Laravel, but if you happen to be using Laravel you can enjoy a few Laravel-specific features.

The package will be auto-discovered by newer Laravel versions, so the only thing left to do is to publish the config file

php artisan vendor:publish --provider="Geocodio\GeocodioServiceProvider"

You can now go ahead and edit your config file at config/geocodio.php.

You will now be able to use the Geocodio facade, or dependency inject the fully-configured Geocodio class.

// Using facadeuseGeocodio;
$response = Geocodio::geocode('1109 N Highland St, Arlington, VA');
// Using dependency injectionuseGeocodio\Geocodio;
class SomeController {
publicfunction__construct(Geocodio$geocoder) {
$response = $geocoder->geocode('1109 N Highland St, Arlington, VA');
}
}

Testing

$ composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Security

If you discover any security related issues, please email security@geocod.io instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.

About

geocod.io PHP library

Resources

Stars

106 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Repository files navigation

geocod.io PHP library Latest VersionTotal Downloads

Library for performing forward and reverse address geocoding for addresses or coordinates in the US, Canada, Mexico and the UK.

Installation

You can install the package via composer:

composer require geocodio/geocodio-library-php

Using Laravel? Great! There's an optional Laravel service provider, for easy integration into your app.

Usage

Don't have an API key yet? Sign up at https://dash.geocod.io to get an API key. The first 2,500 lookups per day are free.

Single geocoding

Using the Laravel integration? Check out Laravel-specific usage examples below.

$geocoder = newGeocodio\Geocodio();
$geocoder->setApiKey('YOUR_API_KEY');
// $geocoder->setHostname('api-hipaa.geocod.io'); // optionally overwrite the API hostname$response = $geocoder->geocode('1109 N Highland St, Arlington, VA');
dump($response);
/*array:1 [ "results" => array:1 [ 0 => array:6 [ "address_components" => array:10 [ "number" => "1109" "predirectional" => "N" "street" => "Highland" "suffix" => "St" "formatted_street" => "N Highland St" "city" => "Arlington" "county" => "Arlington County" "state_province" => "VA" "postal_code" => "22201" "country" => "US" ] "formatted_address" => "1109 N Highland St, Arlington, VA 22201" "location" => array:2 [ "lat" => 38.886672 "lng" => -77.094735 ] "accuracy" => 1 "accuracy_type" => "rooftop" "source" => "Arlington" ] ]]*/$response = $geocoder->reverse('38.9002898,-76.9990361');
$response = $geocoder->reverse([38.9002898, -76.9990361]);

Note: You can read more about accuracy scores, accuracy types, input formats and more at https://www.geocod.io/docs/

Batch geocoding

To batch geocode, simply pass an array of addresses or coordinates instead of a single string

$response = $geocoder->geocode([
'1109 N Highland St, Arlington VA',
'525 University Ave, Toronto, ON, Canada',
'10 Downing St, London, United Kingdom',
'4410 S Highway 17 92, Casselberry FL',
'15000 NE 24th Street, Redmond WA',
'17015 Walnut Grove Drive, Morgan Hill CA'
]);
$response = $geocoder->reverse([
'35.9746000,-77.9658000',
'32.8793700,-96.6303900',
'33.8337100,-117.8362320',
'35.4171240,-80.6784760'
]);
// Optionally supply a custom key that will be returned along with results$response = $geocoder->geocode([
'MyId1' => '1109 N Highland St, Arlington VA',
'MyId2' => '525 University Ave, Toronto, ON, Canada',
'MyId3' => '10 Downing St, London, United Kingdom',
'MyId4' => '4410 S Highway 17 92, Casselberry FL',
'MyId5' => '15000 NE 24th Street, Redmond WA',
'MyId6' => '17015 Walnut Grove Drive, Morgan Hill CA'
]);

Field appends

Geocodio allows you to append additional data points such as congressional districts, census codes, timezone, ACS survey results, UK constituencies and wards, and much much more.

To request additional fields, simply supply them as an array as the second parameter

$response = $geocoder->geocode(
[
'1109 N Highland St, Arlington VA',
'525 University Ave, Toronto, ON, Canada'
],
[
'cd',
'timezone'
]
);
$response = $geocoder->reverse('38.9002898,-76.9990361', ['census2010']);
// United Kingdom addresses support UK-specific appends such as Westminster and// devolved parliament constituencies, and local authority wards$response = $geocoder->geocode(
'10 Downing St, London, United Kingdom',
['uk-westminster', 'uk-local']
);

Address components

For forward geocoding requests it is possible to supply individual address components instead of a full address string. This works for both single and batch geocoding requests.

$response = $geocoder->geocode([
'street' => '1109 N Highland St',
'city' => 'Arlington',
'state_province' => 'VA',
'postal_code' => '22201'
]);
$response = $geocoder->geocode([
[
'street' => '1109 N Highland St',
'city' => 'Arlington',
'state_province' => 'VA'
],
[
'street' => '525 University Ave',
'city' => 'Toronto',
'state_province' => 'ON',
'country' => 'Canada',
],
[
'street' => '10 Downing St',
'city' => 'London',
'postal_code' => 'SW1A 2AA',
'country' => 'United Kingdom',
],
]);

Limit results

Optionally limit the number of maximum geocoding results by using the third parameter on geocode(...) or reverse(...)

$response = $geocoder->geocode('1109 N Highland St, Arlington, VA', [], 1); // Only get the first result$response = $geocoder->reverse('38.9002898,-76.9990361', ['timezone'], 5); // Return up to 5 geocoding results

Uploading Lists

The lists API lets you upload and process spreadsheet with addresses or coordinates. Similar to the spreadsheet feature in the dashboard, the spreadsheet will be processed as a job on Geocodio's infrastructure and can be downloaded at a later time. While a spreadsheet is being processed it is possible to query the status and progress.

Important

Data for spreadsheets processed through the lists API are automatically deleted 72 hours after they have finished processing. In addition to a 1GB file size limit, we recommend a maximum of 10M lookups per list batch. Larger batches should be split up into multiple list jobs.

See the API docs for geocoding lists for additional details.

Upload list from a file

Creates a new spreadsheet list job and starts processing the list in the background. The response returns a list id that can be used to retrieve the job progress as well as download the processed list when it has completed.

$response = $geocoder->uploadList(
file: 'path/to/file.csv',
direction: GeocodeDirection::Forward,
format: '{{B}} {{C}} {{D}} {{E}}',
callbackWebhook: 'https://example.com/callbacks/list-upload',
);
/*array:2 [ "id" => 11953719 "file" => array:3 [ "headers" => array:5 [ 0 => "Name" 1 => "Address" 2 => "City" 3 => "State" 4 => "Zip" ] "estimated_rows_count" => 4 "filename" => "simple.csv" ]]*/

Upload list of inline data

$csvData = <<<'CSV'name,street,city,state,zip"Peregrine Espresso","660 Pennsylvania Ave SE",Washington,DC,20003"Lot 38 Espresso Bar","1001 2nd St SE",Washington,DC,20003
CSV;
$geocodio->uploadInlineList(
$csvData,
'coffee-shops.csv',
GeocodeDirection::Forward,
'{{B}} {{C}} {{D}} {{E}}'
);

List status

View the metadata and status for a single uploaded list.

$geocoder->listStatus(11950669);
/*array:6 [ "id" => 11953719 "fields" => [] "file" => array:2 [ "estimated_rows_count" => 4 "filename" => "simple.csv" ] "status" => array:5 [ "state" => "COMPLETED" "progress" => 100 "message" => "Completed" "time_left_description" => null "time_left_seconds" => null ] "download_url" => "https://api.geocod.io/v2/lists/11953719/download" "expires_at" => "2025-08-22T20:36:10.000000Z"]*/

Download a list

Download a fully geocoded list, the returned format will always be a UTF-8 encoded, comma-separated csv file.

$geocoder->downloadList(11950669, 'path/to/file.csv');

Fetch all uploaded lists

Show all lists that have been created. The endpoint is paginated, showing 15 lists at a time, ordered by recency.

$geocoder->lists();
/*array:9 [ "current_page" => 1 "data" => array:1 [ 0 => array:6 [ "id" => 11953719 "fields" => [] "file" => array:2 [ "estimated_rows_count" => 4 "filename" => "simple.csv" ] "status" => array:5 [ "state" => "COMPLETED" "progress" => 100 "message" => "Completed" "time_left_description" => null "time_left_seconds" => null ] "download_url" => "https://api.geocod.io/v2/lists/11953719/download" "expires_at" => "2025-08-22T20:36:10.000000Z" ] "first_page_url" => "https://api.geocod.io/v2/lists?page=1" "from" => 1 "next_page_url" => null "path" => "https://api.geocod.io/v2/lists" "per_page" => 15 "prev_page_url" => null "to" => 3]*/

Delete uploaded list

Delete a previously uploaded list and its underlying spreadsheet data permanently. This can also be used to cancel and delete a spreadsheet that is currently processing.

Geocodio Unlimited customers can cancel a spreadsheet at any time. Pay as You Go customers can only cancel a spreadsheet if it was just recently started.

The spreadsheet data will always be deleted automatically after 72 hours if it is not deleted manually first.

$geocoder->deleteList(11950669);

Distance calculations

Calculate distances from a single origin to multiple destinations, or compute full distance matrices.

Coordinate format with custom IDs

You can add custom identifiers to coordinates using the lat,lng,id format. The ID will be returned in the response, making it easy to match results back to your data:

// String format with ID'37.7749,-122.4194,warehouse_1'// Array format with ID
[37.7749, -122.4194, 'warehouse_1']
// The ID is returned in the response:// [// 'query' => '37.7749,-122.4194,warehouse_1',// 'location' => [37.7749, -122.4194],// 'id' => 'warehouse_1',// 'distance_miles' => 3.2,// 'distance_km' => 5.1// ]

Distance mode and units

The SDK provides enums for type-safe distance configuration:

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Available modes
DistanceMode::Straightline // Default - great-circle (as the crow flies)
DistanceMode::Driving // Road network routing with duration// Available units
DistanceUnits::Miles // Default
DistanceUnits::Km
// Sorting options
DistanceOrderBy::Distance // Default
DistanceOrderBy::Duration
DistanceSortOrder::Asc // Default
DistanceSortOrder::Desc

Note: The default mode is straightline (great-circle distance). Use DistanceMode::Driving if you need road network routing with duration estimates.

Add distance to geocoding requests

You can add distance calculations to existing geocode or reverse geocode requests. Each geocoded result will include a destinations array with distances to each destination.

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Geocode an address and calculate distances to store locations$response = $geocoder->geocode(
'1600 Pennsylvania Ave NW, Washington DC',
destinations: [
'38.9072,-77.0369,store_dc',
'39.2904,-76.6122,store_baltimore',
'39.9526,-75.1652,store_philly'
],
distanceMode: DistanceMode::Driving,
distanceUnits: DistanceUnits::Miles
);
/*Response includes destinations for each geocoded result:[ 'results' => [ [ 'formatted_address' => '1600 Pennsylvania Ave NW, Washington, DC 20500', 'location' => ['lat' => 38.8977, 'lng' => -77.0365], 'destinations' => [ [ 'query' => '38.9072,-77.0369,store_dc', 'location' => [38.9072, -77.0369], 'id' => 'store_dc', 'distance_miles' => 0.8, 'distance_km' => 1.3, 'duration_seconds' => 180 ], [ 'query' => '39.2904,-76.6122,store_baltimore', 'location' => [39.2904, -76.6122], 'id' => 'store_baltimore', 'distance_miles' => 38.2, 'distance_km' => 61.5, 'duration_seconds' => 2820 ], // ... ] ] ]]*/// Reverse geocode with distances$response = $geocoder->reverse(
query: '38.8977,-77.0365',
destinations: [
'38.9072,-77.0369,capitol',
'38.8895,-77.0353,monument'
],
distanceMode: DistanceMode::Straightline
);
// With filtering - find nearest 3 stores within 50 miles$response = $geocoder->geocode(
'1600 Pennsylvania Ave NW, Washington DC',
destinations: [
'38.9072,-77.0369,store_1',
'39.2904,-76.6122,store_2',
'39.9526,-75.1652,store_3',
'40.7128,-74.0060,store_4'
],
distanceMode: DistanceMode::Driving,
distanceMaxResults: 3,
distanceMaxDistance: 50.0,
distanceOrderBy: DistanceOrderBy::Distance,
distanceSortOrder: DistanceSortOrder::Asc
);

Single origin to multiple destinations

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Calculate distances from one origin to multiple destinations$response = $geocoder->distance(
'37.7749,-122.4194,headquarters', // Origin with ID
[
'37.7849,-122.4094,customer_a',
'37.7949,-122.3994,customer_b',
'37.8049,-122.4294,customer_c'
]
);
/*Response:[ 'origin' => [ 'query' => '37.7749,-122.4194,headquarters', 'location' => [37.7749, -122.4194], 'id' => 'headquarters' ], 'destinations' => [ [ 'query' => '37.7849,-122.4094,customer_a', 'location' => [37.7849, -122.4094], 'id' => 'customer_a', 'distance_miles' => 0.9, 'distance_km' => 1.4 ], // ... ]]*/// Use driving mode for road network routing (includes duration)$response = $geocoder->distance(
'37.7749,-122.4194',
['37.7849,-122.4094'],
mode: DistanceMode::Driving
);
// With all filtering and sorting options$response = $geocoder->distance(
origin: '37.7749,-122.4194,warehouse',
destinations: [
'37.7849,-122.4094,store_1',
'37.7949,-122.3994,store_2',
'37.8049,-122.4294,store_3'
],
mode: DistanceMode::Driving,
units: DistanceUnits::Km,
maxResults: 2,
maxDistance: 10.0,
orderBy: DistanceOrderBy::Distance,
sortOrder: DistanceSortOrder::Asc
);
// Array format for coordinates (with or without ID)$response = $geocoder->distance(
[37.7749, -122.4194], // Without ID
[[37.7849, -122.4094, 'dest_1']] // With ID as third element
);

Distance matrix (multiple origins × destinations)

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Calculate full distance matrix with custom IDs$response = $geocoder->distanceMatrix(
origins: [
'37.7749,-122.4194,warehouse_sf',
'37.8049,-122.4294,warehouse_oak'
],
destinations: [
'37.7849,-122.4094,customer_1',
'37.7949,-122.3994,customer_2'
]
);
/*Response structure:[ 'results' => [ [ 'origin' => [ 'query' => '37.7749,-122.4194,warehouse_sf', 'location' => [37.7749, -122.4194], 'id' => 'warehouse_sf' ], 'destinations' => [ [ 'query' => '37.7849,-122.4094,customer_1', 'location' => [37.7849, -122.4094], 'id' => 'customer_1', 'distance_miles' => 0.9, 'distance_km' => 1.4 ], // ... ] ], [ 'origin' => [..., 'id' => 'warehouse_oak'], 'destinations' => [...] ] ]]*/// With driving mode and kilometers$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: ['37.7849,-122.4094'],
mode: DistanceMode::Driving,
units: DistanceUnits::Km
);

Nearest mode (find closest destinations)

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Find up to 2 nearest destinations from each origin$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: ['37.7849,-122.4094', '37.7949,-122.3994', '37.8049,-122.4294'],
maxResults: 2
);
// Filter by maximum distance (in miles or km depending on units)$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: [...],
maxDistance: 2.0
);
// Filter by minimum and maximum distance$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: [...],
minDistance: 1.0,
maxDistance: 10.0
);
// Filter by duration (seconds, driving mode only)$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: [...],
mode: DistanceMode::Driving,
maxDuration: 300, // 5 minutes
minDuration: 60// 1 minute minimum
);
// Sort by duration descending$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: [...],
mode: DistanceMode::Driving,
maxResults: 5,
orderBy: DistanceOrderBy::Duration,
sortOrder: DistanceSortOrder::Desc
);

Async Distance Matrix Jobs

For large distance matrix calculations, use async jobs that process in the background.

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
// Create a new distance matrix job$job = $geocoder->createDistanceMatrixJob(
name: 'My Distance Calculation',
origins: ['37.7749,-122.4194', '37.8049,-122.4294'],
destinations: ['37.7849,-122.4094', '37.7949,-122.3994'],
mode: DistanceMode::Driving,
units: DistanceUnits::Miles,
callbackUrl: 'https://example.com/webhook'// Optional
);
// Or use list IDs from previously uploaded lists$job = $geocoder->createDistanceMatrixJob(
name: 'Distance from List',
origins: 12345, // List ID
destinations: 67890, // List ID
mode: DistanceMode::Straightline
);
echo$job['id']; // Job identifier// Check job status$status = $geocoder->distanceMatrixJobStatus($job['id']);
/*[ 'id' => 'abc123', 'name' => 'My Distance Calculation', 'status' => [ 'state' => 'COMPLETED', // or 'PROCESSING', 'FAILED' 'progress' => 100, 'message' => 'Completed' ], 'download_url' => 'https://api.geocod.io/v2/distance-matrix/abc123/download', 'expires_at' => '2025-01-15T12:00:00.000000Z']*/// List all jobs$jobs = $geocoder->distanceMatrixJobs();
$jobs = $geocoder->distanceMatrixJobs(page: 2); // Paginated// Get job results as parsed array (same format as distance POST)$results = $geocoder->getDistanceMatrixJobResults($job['id']);
/*[ 'results' => [ [ 'origin' => ['query' => '...', 'location' => [...], 'id' => '...'], 'destinations' => [ ['query' => '...', 'location' => [...], 'id' => '...', 'distance_miles' => 1.2, ...] ] ], // ... ]]*/// Or download to file for very large results$geocoder->downloadDistanceMatrixJob($job['id'], '/path/to/results.json');
// Delete a job$geocoder->deleteDistanceMatrixJob($job['id']);

Note: Distance lookups are billed separately, please refer to https://www.geocod.io/pricing/ for more information.

Usage with Laravel

This library works well without Laravel, but if you happen to be using Laravel you can enjoy a few Laravel-specific features.

The package will be auto-discovered by newer Laravel versions, so the only thing left to do is to publish the config file

php artisan vendor:publish --provider="Geocodio\GeocodioServiceProvider"

You can now go ahead and edit your config file at config/geocodio.php.

You will now be able to use the Geocodio facade, or dependency inject the fully-configured Geocodio class.

// Using facadeuseGeocodio;
$response = Geocodio::geocode('1109 N Highland St, Arlington, VA');
// Using dependency injectionuseGeocodio\Geocodio;
class SomeController {
publicfunction__construct(Geocodio$geocoder) {
$response = $geocoder->geocode('1109 N Highland St, Arlington, VA');
}
}

Testing

$ composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Security

If you discover any security related issues, please email security@geocod.io instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.

About

geocod.io PHP library

Resources

Stars

106 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Repository files navigation

geocod.io PHP library Latest VersionTotal Downloads

Library for performing forward and reverse address geocoding for addresses or coordinates in the US, Canada, Mexico and the UK.

Installation

You can install the package via composer:

composer require geocodio/geocodio-library-php

Using Laravel? Great! There's an optional Laravel service provider, for easy integration into your app.

Usage

Don't have an API key yet? Sign up at https://dash.geocod.io to get an API key. The first 2,500 lookups per day are free.

Single geocoding

Using the Laravel integration? Check out Laravel-specific usage examples below.

$geocoder = newGeocodio\Geocodio();
$geocoder->setApiKey('YOUR_API_KEY');
// $geocoder->setHostname('api-hipaa.geocod.io'); // optionally overwrite the API hostname$response = $geocoder->geocode('1109 N Highland St, Arlington, VA');
dump($response);
/*array:1 [ "results" => array:1 [ 0 => array:6 [ "address_components" => array:10 [ "number" => "1109" "predirectional" => "N" "street" => "Highland" "suffix" => "St" "formatted_street" => "N Highland St" "city" => "Arlington" "county" => "Arlington County" "state_province" => "VA" "postal_code" => "22201" "country" => "US" ] "formatted_address" => "1109 N Highland St, Arlington, VA 22201" "location" => array:2 [ "lat" => 38.886672 "lng" => -77.094735 ] "accuracy" => 1 "accuracy_type" => "rooftop" "source" => "Arlington" ] ]]*/$response = $geocoder->reverse('38.9002898,-76.9990361');
$response = $geocoder->reverse([38.9002898, -76.9990361]);

Note: You can read more about accuracy scores, accuracy types, input formats and more at https://www.geocod.io/docs/

Batch geocoding

To batch geocode, simply pass an array of addresses or coordinates instead of a single string

$response = $geocoder->geocode([
'1109 N Highland St, Arlington VA',
'525 University Ave, Toronto, ON, Canada',
'10 Downing St, London, United Kingdom',
'4410 S Highway 17 92, Casselberry FL',
'15000 NE 24th Street, Redmond WA',
'17015 Walnut Grove Drive, Morgan Hill CA'
]);
$response = $geocoder->reverse([
'35.9746000,-77.9658000',
'32.8793700,-96.6303900',
'33.8337100,-117.8362320',
'35.4171240,-80.6784760'
]);
// Optionally supply a custom key that will be returned along with results$response = $geocoder->geocode([
'MyId1' => '1109 N Highland St, Arlington VA',
'MyId2' => '525 University Ave, Toronto, ON, Canada',
'MyId3' => '10 Downing St, London, United Kingdom',
'MyId4' => '4410 S Highway 17 92, Casselberry FL',
'MyId5' => '15000 NE 24th Street, Redmond WA',
'MyId6' => '17015 Walnut Grove Drive, Morgan Hill CA'
]);

Field appends

Geocodio allows you to append additional data points such as congressional districts, census codes, timezone, ACS survey results, UK constituencies and wards, and much much more.

To request additional fields, simply supply them as an array as the second parameter

$response = $geocoder->geocode(
[
'1109 N Highland St, Arlington VA',
'525 University Ave, Toronto, ON, Canada'
],
[
'cd',
'timezone'
]
);
$response = $geocoder->reverse('38.9002898,-76.9990361', ['census2010']);
// United Kingdom addresses support UK-specific appends such as Westminster and// devolved parliament constituencies, and local authority wards$response = $geocoder->geocode(
'10 Downing St, London, United Kingdom',
['uk-westminster', 'uk-local']
);

Address components

For forward geocoding requests it is possible to supply individual address components instead of a full address string. This works for both single and batch geocoding requests.

$response = $geocoder->geocode([
'street' => '1109 N Highland St',
'city' => 'Arlington',
'state_province' => 'VA',
'postal_code' => '22201'
]);
$response = $geocoder->geocode([
[
'street' => '1109 N Highland St',
'city' => 'Arlington',
'state_province' => 'VA'
],
[
'street' => '525 University Ave',
'city' => 'Toronto',
'state_province' => 'ON',
'country' => 'Canada',
],
[
'street' => '10 Downing St',
'city' => 'London',
'postal_code' => 'SW1A 2AA',
'country' => 'United Kingdom',
],
]);

Limit results

Optionally limit the number of maximum geocoding results by using the third parameter on geocode(...) or reverse(...)

$response = $geocoder->geocode('1109 N Highland St, Arlington, VA', [], 1); // Only get the first result$response = $geocoder->reverse('38.9002898,-76.9990361', ['timezone'], 5); // Return up to 5 geocoding results

Uploading Lists

The lists API lets you upload and process spreadsheet with addresses or coordinates. Similar to the spreadsheet feature in the dashboard, the spreadsheet will be processed as a job on Geocodio's infrastructure and can be downloaded at a later time. While a spreadsheet is being processed it is possible to query the status and progress.

Important

Data for spreadsheets processed through the lists API are automatically deleted 72 hours after they have finished processing. In addition to a 1GB file size limit, we recommend a maximum of 10M lookups per list batch. Larger batches should be split up into multiple list jobs.

See the API docs for geocoding lists for additional details.

Upload list from a file

Creates a new spreadsheet list job and starts processing the list in the background. The response returns a list id that can be used to retrieve the job progress as well as download the processed list when it has completed.

$response = $geocoder->uploadList(
file: 'path/to/file.csv',
direction: GeocodeDirection::Forward,
format: '{{B}} {{C}} {{D}} {{E}}',
callbackWebhook: 'https://example.com/callbacks/list-upload',
);
/*array:2 [ "id" => 11953719 "file" => array:3 [ "headers" => array:5 [ 0 => "Name" 1 => "Address" 2 => "City" 3 => "State" 4 => "Zip" ] "estimated_rows_count" => 4 "filename" => "simple.csv" ]]*/

Upload list of inline data

$csvData = <<<'CSV'name,street,city,state,zip"Peregrine Espresso","660 Pennsylvania Ave SE",Washington,DC,20003"Lot 38 Espresso Bar","1001 2nd St SE",Washington,DC,20003
CSV;
$geocodio->uploadInlineList(
$csvData,
'coffee-shops.csv',
GeocodeDirection::Forward,
'{{B}} {{C}} {{D}} {{E}}'
);

List status

View the metadata and status for a single uploaded list.

$geocoder->listStatus(11950669);
/*array:6 [ "id" => 11953719 "fields" => [] "file" => array:2 [ "estimated_rows_count" => 4 "filename" => "simple.csv" ] "status" => array:5 [ "state" => "COMPLETED" "progress" => 100 "message" => "Completed" "time_left_description" => null "time_left_seconds" => null ] "download_url" => "https://api.geocod.io/v2/lists/11953719/download" "expires_at" => "2025-08-22T20:36:10.000000Z"]*/

Download a list

Download a fully geocoded list, the returned format will always be a UTF-8 encoded, comma-separated csv file.

$geocoder->downloadList(11950669, 'path/to/file.csv');

Fetch all uploaded lists

Show all lists that have been created. The endpoint is paginated, showing 15 lists at a time, ordered by recency.

$geocoder->lists();
/*array:9 [ "current_page" => 1 "data" => array:1 [ 0 => array:6 [ "id" => 11953719 "fields" => [] "file" => array:2 [ "estimated_rows_count" => 4 "filename" => "simple.csv" ] "status" => array:5 [ "state" => "COMPLETED" "progress" => 100 "message" => "Completed" "time_left_description" => null "time_left_seconds" => null ] "download_url" => "https://api.geocod.io/v2/lists/11953719/download" "expires_at" => "2025-08-22T20:36:10.000000Z" ] "first_page_url" => "https://api.geocod.io/v2/lists?page=1" "from" => 1 "next_page_url" => null "path" => "https://api.geocod.io/v2/lists" "per_page" => 15 "prev_page_url" => null "to" => 3]*/

Delete uploaded list

Delete a previously uploaded list and its underlying spreadsheet data permanently. This can also be used to cancel and delete a spreadsheet that is currently processing.

Geocodio Unlimited customers can cancel a spreadsheet at any time. Pay as You Go customers can only cancel a spreadsheet if it was just recently started.

The spreadsheet data will always be deleted automatically after 72 hours if it is not deleted manually first.

$geocoder->deleteList(11950669);

Distance calculations

Calculate distances from a single origin to multiple destinations, or compute full distance matrices.

Coordinate format with custom IDs

You can add custom identifiers to coordinates using the lat,lng,id format. The ID will be returned in the response, making it easy to match results back to your data:

// String format with ID'37.7749,-122.4194,warehouse_1'// Array format with ID
[37.7749, -122.4194, 'warehouse_1']
// The ID is returned in the response:// [// 'query' => '37.7749,-122.4194,warehouse_1',// 'location' => [37.7749, -122.4194],// 'id' => 'warehouse_1',// 'distance_miles' => 3.2,// 'distance_km' => 5.1// ]

Distance mode and units

The SDK provides enums for type-safe distance configuration:

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Available modes
DistanceMode::Straightline // Default - great-circle (as the crow flies)
DistanceMode::Driving // Road network routing with duration// Available units
DistanceUnits::Miles // Default
DistanceUnits::Km
// Sorting options
DistanceOrderBy::Distance // Default
DistanceOrderBy::Duration
DistanceSortOrder::Asc // Default
DistanceSortOrder::Desc

Note: The default mode is straightline (great-circle distance). Use DistanceMode::Driving if you need road network routing with duration estimates.

Add distance to geocoding requests

You can add distance calculations to existing geocode or reverse geocode requests. Each geocoded result will include a destinations array with distances to each destination.

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Geocode an address and calculate distances to store locations$response = $geocoder->geocode(
'1600 Pennsylvania Ave NW, Washington DC',
destinations: [
'38.9072,-77.0369,store_dc',
'39.2904,-76.6122,store_baltimore',
'39.9526,-75.1652,store_philly'
],
distanceMode: DistanceMode::Driving,
distanceUnits: DistanceUnits::Miles
);
/*Response includes destinations for each geocoded result:[ 'results' => [ [ 'formatted_address' => '1600 Pennsylvania Ave NW, Washington, DC 20500', 'location' => ['lat' => 38.8977, 'lng' => -77.0365], 'destinations' => [ [ 'query' => '38.9072,-77.0369,store_dc', 'location' => [38.9072, -77.0369], 'id' => 'store_dc', 'distance_miles' => 0.8, 'distance_km' => 1.3, 'duration_seconds' => 180 ], [ 'query' => '39.2904,-76.6122,store_baltimore', 'location' => [39.2904, -76.6122], 'id' => 'store_baltimore', 'distance_miles' => 38.2, 'distance_km' => 61.5, 'duration_seconds' => 2820 ], // ... ] ] ]]*/// Reverse geocode with distances$response = $geocoder->reverse(
query: '38.8977,-77.0365',
destinations: [
'38.9072,-77.0369,capitol',
'38.8895,-77.0353,monument'
],
distanceMode: DistanceMode::Straightline
);
// With filtering - find nearest 3 stores within 50 miles$response = $geocoder->geocode(
'1600 Pennsylvania Ave NW, Washington DC',
destinations: [
'38.9072,-77.0369,store_1',
'39.2904,-76.6122,store_2',
'39.9526,-75.1652,store_3',
'40.7128,-74.0060,store_4'
],
distanceMode: DistanceMode::Driving,
distanceMaxResults: 3,
distanceMaxDistance: 50.0,
distanceOrderBy: DistanceOrderBy::Distance,
distanceSortOrder: DistanceSortOrder::Asc
);

Single origin to multiple destinations

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Calculate distances from one origin to multiple destinations$response = $geocoder->distance(
'37.7749,-122.4194,headquarters', // Origin with ID
[
'37.7849,-122.4094,customer_a',
'37.7949,-122.3994,customer_b',
'37.8049,-122.4294,customer_c'
]
);
/*Response:[ 'origin' => [ 'query' => '37.7749,-122.4194,headquarters', 'location' => [37.7749, -122.4194], 'id' => 'headquarters' ], 'destinations' => [ [ 'query' => '37.7849,-122.4094,customer_a', 'location' => [37.7849, -122.4094], 'id' => 'customer_a', 'distance_miles' => 0.9, 'distance_km' => 1.4 ], // ... ]]*/// Use driving mode for road network routing (includes duration)$response = $geocoder->distance(
'37.7749,-122.4194',
['37.7849,-122.4094'],
mode: DistanceMode::Driving
);
// With all filtering and sorting options$response = $geocoder->distance(
origin: '37.7749,-122.4194,warehouse',
destinations: [
'37.7849,-122.4094,store_1',
'37.7949,-122.3994,store_2',
'37.8049,-122.4294,store_3'
],
mode: DistanceMode::Driving,
units: DistanceUnits::Km,
maxResults: 2,
maxDistance: 10.0,
orderBy: DistanceOrderBy::Distance,
sortOrder: DistanceSortOrder::Asc
);
// Array format for coordinates (with or without ID)$response = $geocoder->distance(
[37.7749, -122.4194], // Without ID
[[37.7849, -122.4094, 'dest_1']] // With ID as third element
);

Distance matrix (multiple origins × destinations)

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Calculate full distance matrix with custom IDs$response = $geocoder->distanceMatrix(
origins: [
'37.7749,-122.4194,warehouse_sf',
'37.8049,-122.4294,warehouse_oak'
],
destinations: [
'37.7849,-122.4094,customer_1',
'37.7949,-122.3994,customer_2'
]
);
/*Response structure:[ 'results' => [ [ 'origin' => [ 'query' => '37.7749,-122.4194,warehouse_sf', 'location' => [37.7749, -122.4194], 'id' => 'warehouse_sf' ], 'destinations' => [ [ 'query' => '37.7849,-122.4094,customer_1', 'location' => [37.7849, -122.4094], 'id' => 'customer_1', 'distance_miles' => 0.9, 'distance_km' => 1.4 ], // ... ] ], [ 'origin' => [..., 'id' => 'warehouse_oak'], 'destinations' => [...] ] ]]*/// With driving mode and kilometers$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: ['37.7849,-122.4094'],
mode: DistanceMode::Driving,
units: DistanceUnits::Km
);

Nearest mode (find closest destinations)

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Find up to 2 nearest destinations from each origin$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: ['37.7849,-122.4094', '37.7949,-122.3994', '37.8049,-122.4294'],
maxResults: 2
);
// Filter by maximum distance (in miles or km depending on units)$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: [...],
maxDistance: 2.0
);
// Filter by minimum and maximum distance$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: [...],
minDistance: 1.0,
maxDistance: 10.0
);
// Filter by duration (seconds, driving mode only)$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: [...],
mode: DistanceMode::Driving,
maxDuration: 300, // 5 minutes
minDuration: 60// 1 minute minimum
);
// Sort by duration descending$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: [...],
mode: DistanceMode::Driving,
maxResults: 5,
orderBy: DistanceOrderBy::Duration,
sortOrder: DistanceSortOrder::Desc
);

Async Distance Matrix Jobs

For large distance matrix calculations, use async jobs that process in the background.

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
// Create a new distance matrix job$job = $geocoder->createDistanceMatrixJob(
name: 'My Distance Calculation',
origins: ['37.7749,-122.4194', '37.8049,-122.4294'],
destinations: ['37.7849,-122.4094', '37.7949,-122.3994'],
mode: DistanceMode::Driving,
units: DistanceUnits::Miles,
callbackUrl: 'https://example.com/webhook'// Optional
);
// Or use list IDs from previously uploaded lists$job = $geocoder->createDistanceMatrixJob(
name: 'Distance from List',
origins: 12345, // List ID
destinations: 67890, // List ID
mode: DistanceMode::Straightline
);
echo$job['id']; // Job identifier// Check job status$status = $geocoder->distanceMatrixJobStatus($job['id']);
/*[ 'id' => 'abc123', 'name' => 'My Distance Calculation', 'status' => [ 'state' => 'COMPLETED', // or 'PROCESSING', 'FAILED' 'progress' => 100, 'message' => 'Completed' ], 'download_url' => 'https://api.geocod.io/v2/distance-matrix/abc123/download', 'expires_at' => '2025-01-15T12:00:00.000000Z']*/// List all jobs$jobs = $geocoder->distanceMatrixJobs();
$jobs = $geocoder->distanceMatrixJobs(page: 2); // Paginated// Get job results as parsed array (same format as distance POST)$results = $geocoder->getDistanceMatrixJobResults($job['id']);
/*[ 'results' => [ [ 'origin' => ['query' => '...', 'location' => [...], 'id' => '...'], 'destinations' => [ ['query' => '...', 'location' => [...], 'id' => '...', 'distance_miles' => 1.2, ...] ] ], // ... ]]*/// Or download to file for very large results$geocoder->downloadDistanceMatrixJob($job['id'], '/path/to/results.json');
// Delete a job$geocoder->deleteDistanceMatrixJob($job['id']);

Note: Distance lookups are billed separately, please refer to https://www.geocod.io/pricing/ for more information.

Usage with Laravel

This library works well without Laravel, but if you happen to be using Laravel you can enjoy a few Laravel-specific features.

The package will be auto-discovered by newer Laravel versions, so the only thing left to do is to publish the config file

php artisan vendor:publish --provider="Geocodio\GeocodioServiceProvider"

You can now go ahead and edit your config file at config/geocodio.php.

You will now be able to use the Geocodio facade, or dependency inject the fully-configured Geocodio class.

// Using facadeuseGeocodio;
$response = Geocodio::geocode('1109 N Highland St, Arlington, VA');
// Using dependency injectionuseGeocodio\Geocodio;
class SomeController {
publicfunction__construct(Geocodio$geocoder) {
$response = $geocoder->geocode('1109 N Highland St, Arlington, VA');
}
}

Testing

$ composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Security

If you discover any security related issues, please email security@geocod.io instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.

About

geocod.io PHP library

Resources

Stars

106 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Repository files navigation

geocod.io PHP library Latest VersionTotal Downloads

Library for performing forward and reverse address geocoding for addresses or coordinates in the US, Canada, Mexico and the UK.

Installation

You can install the package via composer:

composer require geocodio/geocodio-library-php

Using Laravel? Great! There's an optional Laravel service provider, for easy integration into your app.

Usage

Don't have an API key yet? Sign up at https://dash.geocod.io to get an API key. The first 2,500 lookups per day are free.

Single geocoding

Using the Laravel integration? Check out Laravel-specific usage examples below.

$geocoder = newGeocodio\Geocodio();
$geocoder->setApiKey('YOUR_API_KEY');
// $geocoder->setHostname('api-hipaa.geocod.io'); // optionally overwrite the API hostname$response = $geocoder->geocode('1109 N Highland St, Arlington, VA');
dump($response);
/*array:1 [ "results" => array:1 [ 0 => array:6 [ "address_components" => array:10 [ "number" => "1109" "predirectional" => "N" "street" => "Highland" "suffix" => "St" "formatted_street" => "N Highland St" "city" => "Arlington" "county" => "Arlington County" "state_province" => "VA" "postal_code" => "22201" "country" => "US" ] "formatted_address" => "1109 N Highland St, Arlington, VA 22201" "location" => array:2 [ "lat" => 38.886672 "lng" => -77.094735 ] "accuracy" => 1 "accuracy_type" => "rooftop" "source" => "Arlington" ] ]]*/$response = $geocoder->reverse('38.9002898,-76.9990361');
$response = $geocoder->reverse([38.9002898, -76.9990361]);

Note: You can read more about accuracy scores, accuracy types, input formats and more at https://www.geocod.io/docs/

Batch geocoding

To batch geocode, simply pass an array of addresses or coordinates instead of a single string

$response = $geocoder->geocode([
'1109 N Highland St, Arlington VA',
'525 University Ave, Toronto, ON, Canada',
'10 Downing St, London, United Kingdom',
'4410 S Highway 17 92, Casselberry FL',
'15000 NE 24th Street, Redmond WA',
'17015 Walnut Grove Drive, Morgan Hill CA'
]);
$response = $geocoder->reverse([
'35.9746000,-77.9658000',
'32.8793700,-96.6303900',
'33.8337100,-117.8362320',
'35.4171240,-80.6784760'
]);
// Optionally supply a custom key that will be returned along with results$response = $geocoder->geocode([
'MyId1' => '1109 N Highland St, Arlington VA',
'MyId2' => '525 University Ave, Toronto, ON, Canada',
'MyId3' => '10 Downing St, London, United Kingdom',
'MyId4' => '4410 S Highway 17 92, Casselberry FL',
'MyId5' => '15000 NE 24th Street, Redmond WA',
'MyId6' => '17015 Walnut Grove Drive, Morgan Hill CA'
]);

Field appends

Geocodio allows you to append additional data points such as congressional districts, census codes, timezone, ACS survey results, UK constituencies and wards, and much much more.

To request additional fields, simply supply them as an array as the second parameter

$response = $geocoder->geocode(
[
'1109 N Highland St, Arlington VA',
'525 University Ave, Toronto, ON, Canada'
],
[
'cd',
'timezone'
]
);
$response = $geocoder->reverse('38.9002898,-76.9990361', ['census2010']);
// United Kingdom addresses support UK-specific appends such as Westminster and// devolved parliament constituencies, and local authority wards$response = $geocoder->geocode(
'10 Downing St, London, United Kingdom',
['uk-westminster', 'uk-local']
);

Address components

For forward geocoding requests it is possible to supply individual address components instead of a full address string. This works for both single and batch geocoding requests.

$response = $geocoder->geocode([
'street' => '1109 N Highland St',
'city' => 'Arlington',
'state_province' => 'VA',
'postal_code' => '22201'
]);
$response = $geocoder->geocode([
[
'street' => '1109 N Highland St',
'city' => 'Arlington',
'state_province' => 'VA'
],
[
'street' => '525 University Ave',
'city' => 'Toronto',
'state_province' => 'ON',
'country' => 'Canada',
],
[
'street' => '10 Downing St',
'city' => 'London',
'postal_code' => 'SW1A 2AA',
'country' => 'United Kingdom',
],
]);

Limit results

Optionally limit the number of maximum geocoding results by using the third parameter on geocode(...) or reverse(...)

$response = $geocoder->geocode('1109 N Highland St, Arlington, VA', [], 1); // Only get the first result$response = $geocoder->reverse('38.9002898,-76.9990361', ['timezone'], 5); // Return up to 5 geocoding results

Uploading Lists

The lists API lets you upload and process spreadsheet with addresses or coordinates. Similar to the spreadsheet feature in the dashboard, the spreadsheet will be processed as a job on Geocodio's infrastructure and can be downloaded at a later time. While a spreadsheet is being processed it is possible to query the status and progress.

Important

Data for spreadsheets processed through the lists API are automatically deleted 72 hours after they have finished processing. In addition to a 1GB file size limit, we recommend a maximum of 10M lookups per list batch. Larger batches should be split up into multiple list jobs.

See the API docs for geocoding lists for additional details.

Upload list from a file

Creates a new spreadsheet list job and starts processing the list in the background. The response returns a list id that can be used to retrieve the job progress as well as download the processed list when it has completed.

$response = $geocoder->uploadList(
file: 'path/to/file.csv',
direction: GeocodeDirection::Forward,
format: '{{B}} {{C}} {{D}} {{E}}',
callbackWebhook: 'https://example.com/callbacks/list-upload',
);
/*array:2 [ "id" => 11953719 "file" => array:3 [ "headers" => array:5 [ 0 => "Name" 1 => "Address" 2 => "City" 3 => "State" 4 => "Zip" ] "estimated_rows_count" => 4 "filename" => "simple.csv" ]]*/

Upload list of inline data

$csvData = <<<'CSV'name,street,city,state,zip"Peregrine Espresso","660 Pennsylvania Ave SE",Washington,DC,20003"Lot 38 Espresso Bar","1001 2nd St SE",Washington,DC,20003
CSV;
$geocodio->uploadInlineList(
$csvData,
'coffee-shops.csv',
GeocodeDirection::Forward,
'{{B}} {{C}} {{D}} {{E}}'
);

List status

View the metadata and status for a single uploaded list.

$geocoder->listStatus(11950669);
/*array:6 [ "id" => 11953719 "fields" => [] "file" => array:2 [ "estimated_rows_count" => 4 "filename" => "simple.csv" ] "status" => array:5 [ "state" => "COMPLETED" "progress" => 100 "message" => "Completed" "time_left_description" => null "time_left_seconds" => null ] "download_url" => "https://api.geocod.io/v2/lists/11953719/download" "expires_at" => "2025-08-22T20:36:10.000000Z"]*/

Download a list

Download a fully geocoded list, the returned format will always be a UTF-8 encoded, comma-separated csv file.

$geocoder->downloadList(11950669, 'path/to/file.csv');

Fetch all uploaded lists

Show all lists that have been created. The endpoint is paginated, showing 15 lists at a time, ordered by recency.

$geocoder->lists();
/*array:9 [ "current_page" => 1 "data" => array:1 [ 0 => array:6 [ "id" => 11953719 "fields" => [] "file" => array:2 [ "estimated_rows_count" => 4 "filename" => "simple.csv" ] "status" => array:5 [ "state" => "COMPLETED" "progress" => 100 "message" => "Completed" "time_left_description" => null "time_left_seconds" => null ] "download_url" => "https://api.geocod.io/v2/lists/11953719/download" "expires_at" => "2025-08-22T20:36:10.000000Z" ] "first_page_url" => "https://api.geocod.io/v2/lists?page=1" "from" => 1 "next_page_url" => null "path" => "https://api.geocod.io/v2/lists" "per_page" => 15 "prev_page_url" => null "to" => 3]*/

Delete uploaded list

Delete a previously uploaded list and its underlying spreadsheet data permanently. This can also be used to cancel and delete a spreadsheet that is currently processing.

Geocodio Unlimited customers can cancel a spreadsheet at any time. Pay as You Go customers can only cancel a spreadsheet if it was just recently started.

The spreadsheet data will always be deleted automatically after 72 hours if it is not deleted manually first.

$geocoder->deleteList(11950669);

Distance calculations

Calculate distances from a single origin to multiple destinations, or compute full distance matrices.

Coordinate format with custom IDs

You can add custom identifiers to coordinates using the lat,lng,id format. The ID will be returned in the response, making it easy to match results back to your data:

// String format with ID'37.7749,-122.4194,warehouse_1'// Array format with ID
[37.7749, -122.4194, 'warehouse_1']
// The ID is returned in the response:// [// 'query' => '37.7749,-122.4194,warehouse_1',// 'location' => [37.7749, -122.4194],// 'id' => 'warehouse_1',// 'distance_miles' => 3.2,// 'distance_km' => 5.1// ]

Distance mode and units

The SDK provides enums for type-safe distance configuration:

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Available modes
DistanceMode::Straightline // Default - great-circle (as the crow flies)
DistanceMode::Driving // Road network routing with duration// Available units
DistanceUnits::Miles // Default
DistanceUnits::Km
// Sorting options
DistanceOrderBy::Distance // Default
DistanceOrderBy::Duration
DistanceSortOrder::Asc // Default
DistanceSortOrder::Desc

Note: The default mode is straightline (great-circle distance). Use DistanceMode::Driving if you need road network routing with duration estimates.

Add distance to geocoding requests

You can add distance calculations to existing geocode or reverse geocode requests. Each geocoded result will include a destinations array with distances to each destination.

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Geocode an address and calculate distances to store locations$response = $geocoder->geocode(
'1600 Pennsylvania Ave NW, Washington DC',
destinations: [
'38.9072,-77.0369,store_dc',
'39.2904,-76.6122,store_baltimore',
'39.9526,-75.1652,store_philly'
],
distanceMode: DistanceMode::Driving,
distanceUnits: DistanceUnits::Miles
);
/*Response includes destinations for each geocoded result:[ 'results' => [ [ 'formatted_address' => '1600 Pennsylvania Ave NW, Washington, DC 20500', 'location' => ['lat' => 38.8977, 'lng' => -77.0365], 'destinations' => [ [ 'query' => '38.9072,-77.0369,store_dc', 'location' => [38.9072, -77.0369], 'id' => 'store_dc', 'distance_miles' => 0.8, 'distance_km' => 1.3, 'duration_seconds' => 180 ], [ 'query' => '39.2904,-76.6122,store_baltimore', 'location' => [39.2904, -76.6122], 'id' => 'store_baltimore', 'distance_miles' => 38.2, 'distance_km' => 61.5, 'duration_seconds' => 2820 ], // ... ] ] ]]*/// Reverse geocode with distances$response = $geocoder->reverse(
query: '38.8977,-77.0365',
destinations: [
'38.9072,-77.0369,capitol',
'38.8895,-77.0353,monument'
],
distanceMode: DistanceMode::Straightline
);
// With filtering - find nearest 3 stores within 50 miles$response = $geocoder->geocode(
'1600 Pennsylvania Ave NW, Washington DC',
destinations: [
'38.9072,-77.0369,store_1',
'39.2904,-76.6122,store_2',
'39.9526,-75.1652,store_3',
'40.7128,-74.0060,store_4'
],
distanceMode: DistanceMode::Driving,
distanceMaxResults: 3,
distanceMaxDistance: 50.0,
distanceOrderBy: DistanceOrderBy::Distance,
distanceSortOrder: DistanceSortOrder::Asc
);

Single origin to multiple destinations

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Calculate distances from one origin to multiple destinations$response = $geocoder->distance(
'37.7749,-122.4194,headquarters', // Origin with ID
[
'37.7849,-122.4094,customer_a',
'37.7949,-122.3994,customer_b',
'37.8049,-122.4294,customer_c'
]
);
/*Response:[ 'origin' => [ 'query' => '37.7749,-122.4194,headquarters', 'location' => [37.7749, -122.4194], 'id' => 'headquarters' ], 'destinations' => [ [ 'query' => '37.7849,-122.4094,customer_a', 'location' => [37.7849, -122.4094], 'id' => 'customer_a', 'distance_miles' => 0.9, 'distance_km' => 1.4 ], // ... ]]*/// Use driving mode for road network routing (includes duration)$response = $geocoder->distance(
'37.7749,-122.4194',
['37.7849,-122.4094'],
mode: DistanceMode::Driving
);
// With all filtering and sorting options$response = $geocoder->distance(
origin: '37.7749,-122.4194,warehouse',
destinations: [
'37.7849,-122.4094,store_1',
'37.7949,-122.3994,store_2',
'37.8049,-122.4294,store_3'
],
mode: DistanceMode::Driving,
units: DistanceUnits::Km,
maxResults: 2,
maxDistance: 10.0,
orderBy: DistanceOrderBy::Distance,
sortOrder: DistanceSortOrder::Asc
);
// Array format for coordinates (with or without ID)$response = $geocoder->distance(
[37.7749, -122.4194], // Without ID
[[37.7849, -122.4094, 'dest_1']] // With ID as third element
);

Distance matrix (multiple origins × destinations)

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Calculate full distance matrix with custom IDs$response = $geocoder->distanceMatrix(
origins: [
'37.7749,-122.4194,warehouse_sf',
'37.8049,-122.4294,warehouse_oak'
],
destinations: [
'37.7849,-122.4094,customer_1',
'37.7949,-122.3994,customer_2'
]
);
/*Response structure:[ 'results' => [ [ 'origin' => [ 'query' => '37.7749,-122.4194,warehouse_sf', 'location' => [37.7749, -122.4194], 'id' => 'warehouse_sf' ], 'destinations' => [ [ 'query' => '37.7849,-122.4094,customer_1', 'location' => [37.7849, -122.4094], 'id' => 'customer_1', 'distance_miles' => 0.9, 'distance_km' => 1.4 ], // ... ] ], [ 'origin' => [..., 'id' => 'warehouse_oak'], 'destinations' => [...] ] ]]*/// With driving mode and kilometers$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: ['37.7849,-122.4094'],
mode: DistanceMode::Driving,
units: DistanceUnits::Km
);

Nearest mode (find closest destinations)

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Find up to 2 nearest destinations from each origin$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: ['37.7849,-122.4094', '37.7949,-122.3994', '37.8049,-122.4294'],
maxResults: 2
);
// Filter by maximum distance (in miles or km depending on units)$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: [...],
maxDistance: 2.0
);
// Filter by minimum and maximum distance$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: [...],
minDistance: 1.0,
maxDistance: 10.0
);
// Filter by duration (seconds, driving mode only)$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: [...],
mode: DistanceMode::Driving,
maxDuration: 300, // 5 minutes
minDuration: 60// 1 minute minimum
);
// Sort by duration descending$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: [...],
mode: DistanceMode::Driving,
maxResults: 5,
orderBy: DistanceOrderBy::Duration,
sortOrder: DistanceSortOrder::Desc
);

Async Distance Matrix Jobs

For large distance matrix calculations, use async jobs that process in the background.

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
// Create a new distance matrix job$job = $geocoder->createDistanceMatrixJob(
name: 'My Distance Calculation',
origins: ['37.7749,-122.4194', '37.8049,-122.4294'],
destinations: ['37.7849,-122.4094', '37.7949,-122.3994'],
mode: DistanceMode::Driving,
units: DistanceUnits::Miles,
callbackUrl: 'https://example.com/webhook'// Optional
);
// Or use list IDs from previously uploaded lists$job = $geocoder->createDistanceMatrixJob(
name: 'Distance from List',
origins: 12345, // List ID
destinations: 67890, // List ID
mode: DistanceMode::Straightline
);
echo$job['id']; // Job identifier// Check job status$status = $geocoder->distanceMatrixJobStatus($job['id']);
/*[ 'id' => 'abc123', 'name' => 'My Distance Calculation', 'status' => [ 'state' => 'COMPLETED', // or 'PROCESSING', 'FAILED' 'progress' => 100, 'message' => 'Completed' ], 'download_url' => 'https://api.geocod.io/v2/distance-matrix/abc123/download', 'expires_at' => '2025-01-15T12:00:00.000000Z']*/// List all jobs$jobs = $geocoder->distanceMatrixJobs();
$jobs = $geocoder->distanceMatrixJobs(page: 2); // Paginated// Get job results as parsed array (same format as distance POST)$results = $geocoder->getDistanceMatrixJobResults($job['id']);
/*[ 'results' => [ [ 'origin' => ['query' => '...', 'location' => [...], 'id' => '...'], 'destinations' => [ ['query' => '...', 'location' => [...], 'id' => '...', 'distance_miles' => 1.2, ...] ] ], // ... ]]*/// Or download to file for very large results$geocoder->downloadDistanceMatrixJob($job['id'], '/path/to/results.json');
// Delete a job$geocoder->deleteDistanceMatrixJob($job['id']);

Note: Distance lookups are billed separately, please refer to https://www.geocod.io/pricing/ for more information.

Usage with Laravel

This library works well without Laravel, but if you happen to be using Laravel you can enjoy a few Laravel-specific features.

The package will be auto-discovered by newer Laravel versions, so the only thing left to do is to publish the config file

php artisan vendor:publish --provider="Geocodio\GeocodioServiceProvider"

You can now go ahead and edit your config file at config/geocodio.php.

You will now be able to use the Geocodio facade, or dependency inject the fully-configured Geocodio class.

// Using facadeuseGeocodio;
$response = Geocodio::geocode('1109 N Highland St, Arlington, VA');
// Using dependency injectionuseGeocodio\Geocodio;
class SomeController {
publicfunction__construct(Geocodio$geocoder) {
$response = $geocoder->geocode('1109 N Highland St, Arlington, VA');
}
}

Testing

$ composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Security

If you discover any security related issues, please email security@geocod.io instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.

About

geocod.io PHP library

Resources

Stars

106 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Repository files navigation

geocod.io PHP library Latest VersionTotal Downloads

Library for performing forward and reverse address geocoding for addresses or coordinates in the US, Canada, Mexico and the UK.

Installation

You can install the package via composer:

composer require geocodio/geocodio-library-php

Using Laravel? Great! There's an optional Laravel service provider, for easy integration into your app.

Usage

Don't have an API key yet? Sign up at https://dash.geocod.io to get an API key. The first 2,500 lookups per day are free.

Single geocoding

Using the Laravel integration? Check out Laravel-specific usage examples below.

$geocoder = newGeocodio\Geocodio();
$geocoder->setApiKey('YOUR_API_KEY');
// $geocoder->setHostname('api-hipaa.geocod.io'); // optionally overwrite the API hostname$response = $geocoder->geocode('1109 N Highland St, Arlington, VA');
dump($response);
/*array:1 [ "results" => array:1 [ 0 => array:6 [ "address_components" => array:10 [ "number" => "1109" "predirectional" => "N" "street" => "Highland" "suffix" => "St" "formatted_street" => "N Highland St" "city" => "Arlington" "county" => "Arlington County" "state_province" => "VA" "postal_code" => "22201" "country" => "US" ] "formatted_address" => "1109 N Highland St, Arlington, VA 22201" "location" => array:2 [ "lat" => 38.886672 "lng" => -77.094735 ] "accuracy" => 1 "accuracy_type" => "rooftop" "source" => "Arlington" ] ]]*/$response = $geocoder->reverse('38.9002898,-76.9990361');
$response = $geocoder->reverse([38.9002898, -76.9990361]);

Note: You can read more about accuracy scores, accuracy types, input formats and more at https://www.geocod.io/docs/

Batch geocoding

To batch geocode, simply pass an array of addresses or coordinates instead of a single string

$response = $geocoder->geocode([
'1109 N Highland St, Arlington VA',
'525 University Ave, Toronto, ON, Canada',
'10 Downing St, London, United Kingdom',
'4410 S Highway 17 92, Casselberry FL',
'15000 NE 24th Street, Redmond WA',
'17015 Walnut Grove Drive, Morgan Hill CA'
]);
$response = $geocoder->reverse([
'35.9746000,-77.9658000',
'32.8793700,-96.6303900',
'33.8337100,-117.8362320',
'35.4171240,-80.6784760'
]);
// Optionally supply a custom key that will be returned along with results$response = $geocoder->geocode([
'MyId1' => '1109 N Highland St, Arlington VA',
'MyId2' => '525 University Ave, Toronto, ON, Canada',
'MyId3' => '10 Downing St, London, United Kingdom',
'MyId4' => '4410 S Highway 17 92, Casselberry FL',
'MyId5' => '15000 NE 24th Street, Redmond WA',
'MyId6' => '17015 Walnut Grove Drive, Morgan Hill CA'
]);

Field appends

Geocodio allows you to append additional data points such as congressional districts, census codes, timezone, ACS survey results, UK constituencies and wards, and much much more.

To request additional fields, simply supply them as an array as the second parameter

$response = $geocoder->geocode(
[
'1109 N Highland St, Arlington VA',
'525 University Ave, Toronto, ON, Canada'
],
[
'cd',
'timezone'
]
);
$response = $geocoder->reverse('38.9002898,-76.9990361', ['census2010']);
// United Kingdom addresses support UK-specific appends such as Westminster and// devolved parliament constituencies, and local authority wards$response = $geocoder->geocode(
'10 Downing St, London, United Kingdom',
['uk-westminster', 'uk-local']
);

Address components

For forward geocoding requests it is possible to supply individual address components instead of a full address string. This works for both single and batch geocoding requests.

$response = $geocoder->geocode([
'street' => '1109 N Highland St',
'city' => 'Arlington',
'state_province' => 'VA',
'postal_code' => '22201'
]);
$response = $geocoder->geocode([
[
'street' => '1109 N Highland St',
'city' => 'Arlington',
'state_province' => 'VA'
],
[
'street' => '525 University Ave',
'city' => 'Toronto',
'state_province' => 'ON',
'country' => 'Canada',
],
[
'street' => '10 Downing St',
'city' => 'London',
'postal_code' => 'SW1A 2AA',
'country' => 'United Kingdom',
],
]);

Limit results

Optionally limit the number of maximum geocoding results by using the third parameter on geocode(...) or reverse(...)

$response = $geocoder->geocode('1109 N Highland St, Arlington, VA', [], 1); // Only get the first result$response = $geocoder->reverse('38.9002898,-76.9990361', ['timezone'], 5); // Return up to 5 geocoding results

Uploading Lists

The lists API lets you upload and process spreadsheet with addresses or coordinates. Similar to the spreadsheet feature in the dashboard, the spreadsheet will be processed as a job on Geocodio's infrastructure and can be downloaded at a later time. While a spreadsheet is being processed it is possible to query the status and progress.

Important

Data for spreadsheets processed through the lists API are automatically deleted 72 hours after they have finished processing. In addition to a 1GB file size limit, we recommend a maximum of 10M lookups per list batch. Larger batches should be split up into multiple list jobs.

See the API docs for geocoding lists for additional details.

Upload list from a file

Creates a new spreadsheet list job and starts processing the list in the background. The response returns a list id that can be used to retrieve the job progress as well as download the processed list when it has completed.

$response = $geocoder->uploadList(
file: 'path/to/file.csv',
direction: GeocodeDirection::Forward,
format: '{{B}} {{C}} {{D}} {{E}}',
callbackWebhook: 'https://example.com/callbacks/list-upload',
);
/*array:2 [ "id" => 11953719 "file" => array:3 [ "headers" => array:5 [ 0 => "Name" 1 => "Address" 2 => "City" 3 => "State" 4 => "Zip" ] "estimated_rows_count" => 4 "filename" => "simple.csv" ]]*/

Upload list of inline data

$csvData = <<<'CSV'name,street,city,state,zip"Peregrine Espresso","660 Pennsylvania Ave SE",Washington,DC,20003"Lot 38 Espresso Bar","1001 2nd St SE",Washington,DC,20003
CSV;
$geocodio->uploadInlineList(
$csvData,
'coffee-shops.csv',
GeocodeDirection::Forward,
'{{B}} {{C}} {{D}} {{E}}'
);

List status

View the metadata and status for a single uploaded list.

$geocoder->listStatus(11950669);
/*array:6 [ "id" => 11953719 "fields" => [] "file" => array:2 [ "estimated_rows_count" => 4 "filename" => "simple.csv" ] "status" => array:5 [ "state" => "COMPLETED" "progress" => 100 "message" => "Completed" "time_left_description" => null "time_left_seconds" => null ] "download_url" => "https://api.geocod.io/v2/lists/11953719/download" "expires_at" => "2025-08-22T20:36:10.000000Z"]*/

Download a list

Download a fully geocoded list, the returned format will always be a UTF-8 encoded, comma-separated csv file.

$geocoder->downloadList(11950669, 'path/to/file.csv');

Fetch all uploaded lists

Show all lists that have been created. The endpoint is paginated, showing 15 lists at a time, ordered by recency.

$geocoder->lists();
/*array:9 [ "current_page" => 1 "data" => array:1 [ 0 => array:6 [ "id" => 11953719 "fields" => [] "file" => array:2 [ "estimated_rows_count" => 4 "filename" => "simple.csv" ] "status" => array:5 [ "state" => "COMPLETED" "progress" => 100 "message" => "Completed" "time_left_description" => null "time_left_seconds" => null ] "download_url" => "https://api.geocod.io/v2/lists/11953719/download" "expires_at" => "2025-08-22T20:36:10.000000Z" ] "first_page_url" => "https://api.geocod.io/v2/lists?page=1" "from" => 1 "next_page_url" => null "path" => "https://api.geocod.io/v2/lists" "per_page" => 15 "prev_page_url" => null "to" => 3]*/

Delete uploaded list

Delete a previously uploaded list and its underlying spreadsheet data permanently. This can also be used to cancel and delete a spreadsheet that is currently processing.

Geocodio Unlimited customers can cancel a spreadsheet at any time. Pay as You Go customers can only cancel a spreadsheet if it was just recently started.

The spreadsheet data will always be deleted automatically after 72 hours if it is not deleted manually first.

$geocoder->deleteList(11950669);

Distance calculations

Calculate distances from a single origin to multiple destinations, or compute full distance matrices.

Coordinate format with custom IDs

You can add custom identifiers to coordinates using the lat,lng,id format. The ID will be returned in the response, making it easy to match results back to your data:

// String format with ID'37.7749,-122.4194,warehouse_1'// Array format with ID
[37.7749, -122.4194, 'warehouse_1']
// The ID is returned in the response:// [// 'query' => '37.7749,-122.4194,warehouse_1',// 'location' => [37.7749, -122.4194],// 'id' => 'warehouse_1',// 'distance_miles' => 3.2,// 'distance_km' => 5.1// ]

Distance mode and units

The SDK provides enums for type-safe distance configuration:

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Available modes
DistanceMode::Straightline // Default - great-circle (as the crow flies)
DistanceMode::Driving // Road network routing with duration// Available units
DistanceUnits::Miles // Default
DistanceUnits::Km
// Sorting options
DistanceOrderBy::Distance // Default
DistanceOrderBy::Duration
DistanceSortOrder::Asc // Default
DistanceSortOrder::Desc

Note: The default mode is straightline (great-circle distance). Use DistanceMode::Driving if you need road network routing with duration estimates.

Add distance to geocoding requests

You can add distance calculations to existing geocode or reverse geocode requests. Each geocoded result will include a destinations array with distances to each destination.

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Geocode an address and calculate distances to store locations$response = $geocoder->geocode(
'1600 Pennsylvania Ave NW, Washington DC',
destinations: [
'38.9072,-77.0369,store_dc',
'39.2904,-76.6122,store_baltimore',
'39.9526,-75.1652,store_philly'
],
distanceMode: DistanceMode::Driving,
distanceUnits: DistanceUnits::Miles
);
/*Response includes destinations for each geocoded result:[ 'results' => [ [ 'formatted_address' => '1600 Pennsylvania Ave NW, Washington, DC 20500', 'location' => ['lat' => 38.8977, 'lng' => -77.0365], 'destinations' => [ [ 'query' => '38.9072,-77.0369,store_dc', 'location' => [38.9072, -77.0369], 'id' => 'store_dc', 'distance_miles' => 0.8, 'distance_km' => 1.3, 'duration_seconds' => 180 ], [ 'query' => '39.2904,-76.6122,store_baltimore', 'location' => [39.2904, -76.6122], 'id' => 'store_baltimore', 'distance_miles' => 38.2, 'distance_km' => 61.5, 'duration_seconds' => 2820 ], // ... ] ] ]]*/// Reverse geocode with distances$response = $geocoder->reverse(
query: '38.8977,-77.0365',
destinations: [
'38.9072,-77.0369,capitol',
'38.8895,-77.0353,monument'
],
distanceMode: DistanceMode::Straightline
);
// With filtering - find nearest 3 stores within 50 miles$response = $geocoder->geocode(
'1600 Pennsylvania Ave NW, Washington DC',
destinations: [
'38.9072,-77.0369,store_1',
'39.2904,-76.6122,store_2',
'39.9526,-75.1652,store_3',
'40.7128,-74.0060,store_4'
],
distanceMode: DistanceMode::Driving,
distanceMaxResults: 3,
distanceMaxDistance: 50.0,
distanceOrderBy: DistanceOrderBy::Distance,
distanceSortOrder: DistanceSortOrder::Asc
);

Single origin to multiple destinations

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Calculate distances from one origin to multiple destinations$response = $geocoder->distance(
'37.7749,-122.4194,headquarters', // Origin with ID
[
'37.7849,-122.4094,customer_a',
'37.7949,-122.3994,customer_b',
'37.8049,-122.4294,customer_c'
]
);
/*Response:[ 'origin' => [ 'query' => '37.7749,-122.4194,headquarters', 'location' => [37.7749, -122.4194], 'id' => 'headquarters' ], 'destinations' => [ [ 'query' => '37.7849,-122.4094,customer_a', 'location' => [37.7849, -122.4094], 'id' => 'customer_a', 'distance_miles' => 0.9, 'distance_km' => 1.4 ], // ... ]]*/// Use driving mode for road network routing (includes duration)$response = $geocoder->distance(
'37.7749,-122.4194',
['37.7849,-122.4094'],
mode: DistanceMode::Driving
);
// With all filtering and sorting options$response = $geocoder->distance(
origin: '37.7749,-122.4194,warehouse',
destinations: [
'37.7849,-122.4094,store_1',
'37.7949,-122.3994,store_2',
'37.8049,-122.4294,store_3'
],
mode: DistanceMode::Driving,
units: DistanceUnits::Km,
maxResults: 2,
maxDistance: 10.0,
orderBy: DistanceOrderBy::Distance,
sortOrder: DistanceSortOrder::Asc
);
// Array format for coordinates (with or without ID)$response = $geocoder->distance(
[37.7749, -122.4194], // Without ID
[[37.7849, -122.4094, 'dest_1']] // With ID as third element
);

Distance matrix (multiple origins × destinations)

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Calculate full distance matrix with custom IDs$response = $geocoder->distanceMatrix(
origins: [
'37.7749,-122.4194,warehouse_sf',
'37.8049,-122.4294,warehouse_oak'
],
destinations: [
'37.7849,-122.4094,customer_1',
'37.7949,-122.3994,customer_2'
]
);
/*Response structure:[ 'results' => [ [ 'origin' => [ 'query' => '37.7749,-122.4194,warehouse_sf', 'location' => [37.7749, -122.4194], 'id' => 'warehouse_sf' ], 'destinations' => [ [ 'query' => '37.7849,-122.4094,customer_1', 'location' => [37.7849, -122.4094], 'id' => 'customer_1', 'distance_miles' => 0.9, 'distance_km' => 1.4 ], // ... ] ], [ 'origin' => [..., 'id' => 'warehouse_oak'], 'destinations' => [...] ] ]]*/// With driving mode and kilometers$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: ['37.7849,-122.4094'],
mode: DistanceMode::Driving,
units: DistanceUnits::Km
);

Nearest mode (find closest destinations)

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Find up to 2 nearest destinations from each origin$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: ['37.7849,-122.4094', '37.7949,-122.3994', '37.8049,-122.4294'],
maxResults: 2
);
// Filter by maximum distance (in miles or km depending on units)$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: [...],
maxDistance: 2.0
);
// Filter by minimum and maximum distance$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: [...],
minDistance: 1.0,
maxDistance: 10.0
);
// Filter by duration (seconds, driving mode only)$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: [...],
mode: DistanceMode::Driving,
maxDuration: 300, // 5 minutes
minDuration: 60// 1 minute minimum
);
// Sort by duration descending$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: [...],
mode: DistanceMode::Driving,
maxResults: 5,
orderBy: DistanceOrderBy::Duration,
sortOrder: DistanceSortOrder::Desc
);

Async Distance Matrix Jobs

For large distance matrix calculations, use async jobs that process in the background.

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
// Create a new distance matrix job$job = $geocoder->createDistanceMatrixJob(
name: 'My Distance Calculation',
origins: ['37.7749,-122.4194', '37.8049,-122.4294'],
destinations: ['37.7849,-122.4094', '37.7949,-122.3994'],
mode: DistanceMode::Driving,
units: DistanceUnits::Miles,
callbackUrl: 'https://example.com/webhook'// Optional
);
// Or use list IDs from previously uploaded lists$job = $geocoder->createDistanceMatrixJob(
name: 'Distance from List',
origins: 12345, // List ID
destinations: 67890, // List ID
mode: DistanceMode::Straightline
);
echo$job['id']; // Job identifier// Check job status$status = $geocoder->distanceMatrixJobStatus($job['id']);
/*[ 'id' => 'abc123', 'name' => 'My Distance Calculation', 'status' => [ 'state' => 'COMPLETED', // or 'PROCESSING', 'FAILED' 'progress' => 100, 'message' => 'Completed' ], 'download_url' => 'https://api.geocod.io/v2/distance-matrix/abc123/download', 'expires_at' => '2025-01-15T12:00:00.000000Z']*/// List all jobs$jobs = $geocoder->distanceMatrixJobs();
$jobs = $geocoder->distanceMatrixJobs(page: 2); // Paginated// Get job results as parsed array (same format as distance POST)$results = $geocoder->getDistanceMatrixJobResults($job['id']);
/*[ 'results' => [ [ 'origin' => ['query' => '...', 'location' => [...], 'id' => '...'], 'destinations' => [ ['query' => '...', 'location' => [...], 'id' => '...', 'distance_miles' => 1.2, ...] ] ], // ... ]]*/// Or download to file for very large results$geocoder->downloadDistanceMatrixJob($job['id'], '/path/to/results.json');
// Delete a job$geocoder->deleteDistanceMatrixJob($job['id']);

Note: Distance lookups are billed separately, please refer to https://www.geocod.io/pricing/ for more information.

Usage with Laravel

This library works well without Laravel, but if you happen to be using Laravel you can enjoy a few Laravel-specific features.

The package will be auto-discovered by newer Laravel versions, so the only thing left to do is to publish the config file

php artisan vendor:publish --provider="Geocodio\GeocodioServiceProvider"

You can now go ahead and edit your config file at config/geocodio.php.

You will now be able to use the Geocodio facade, or dependency inject the fully-configured Geocodio class.

// Using facadeuseGeocodio;
$response = Geocodio::geocode('1109 N Highland St, Arlington, VA');
// Using dependency injectionuseGeocodio\Geocodio;
class SomeController {
publicfunction__construct(Geocodio$geocoder) {
$response = $geocoder->geocode('1109 N Highland St, Arlington, VA');
}
}

Testing

$ composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Security

If you discover any security related issues, please email security@geocod.io instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.

About

geocod.io PHP library

Resources

Stars

106 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Repository files navigation

geocod.io PHP library Latest VersionTotal Downloads

Library for performing forward and reverse address geocoding for addresses or coordinates in the US, Canada, Mexico and the UK.

Installation

You can install the package via composer:

composer require geocodio/geocodio-library-php

Using Laravel? Great! There's an optional Laravel service provider, for easy integration into your app.

Usage

Don't have an API key yet? Sign up at https://dash.geocod.io to get an API key. The first 2,500 lookups per day are free.

Single geocoding

Using the Laravel integration? Check out Laravel-specific usage examples below.

$geocoder = newGeocodio\Geocodio();
$geocoder->setApiKey('YOUR_API_KEY');
// $geocoder->setHostname('api-hipaa.geocod.io'); // optionally overwrite the API hostname$response = $geocoder->geocode('1109 N Highland St, Arlington, VA');
dump($response);
/*array:1 [ "results" => array:1 [ 0 => array:6 [ "address_components" => array:10 [ "number" => "1109" "predirectional" => "N" "street" => "Highland" "suffix" => "St" "formatted_street" => "N Highland St" "city" => "Arlington" "county" => "Arlington County" "state_province" => "VA" "postal_code" => "22201" "country" => "US" ] "formatted_address" => "1109 N Highland St, Arlington, VA 22201" "location" => array:2 [ "lat" => 38.886672 "lng" => -77.094735 ] "accuracy" => 1 "accuracy_type" => "rooftop" "source" => "Arlington" ] ]]*/$response = $geocoder->reverse('38.9002898,-76.9990361');
$response = $geocoder->reverse([38.9002898, -76.9990361]);

Note: You can read more about accuracy scores, accuracy types, input formats and more at https://www.geocod.io/docs/

Batch geocoding

To batch geocode, simply pass an array of addresses or coordinates instead of a single string

$response = $geocoder->geocode([
'1109 N Highland St, Arlington VA',
'525 University Ave, Toronto, ON, Canada',
'10 Downing St, London, United Kingdom',
'4410 S Highway 17 92, Casselberry FL',
'15000 NE 24th Street, Redmond WA',
'17015 Walnut Grove Drive, Morgan Hill CA'
]);
$response = $geocoder->reverse([
'35.9746000,-77.9658000',
'32.8793700,-96.6303900',
'33.8337100,-117.8362320',
'35.4171240,-80.6784760'
]);
// Optionally supply a custom key that will be returned along with results$response = $geocoder->geocode([
'MyId1' => '1109 N Highland St, Arlington VA',
'MyId2' => '525 University Ave, Toronto, ON, Canada',
'MyId3' => '10 Downing St, London, United Kingdom',
'MyId4' => '4410 S Highway 17 92, Casselberry FL',
'MyId5' => '15000 NE 24th Street, Redmond WA',
'MyId6' => '17015 Walnut Grove Drive, Morgan Hill CA'
]);

Field appends

Geocodio allows you to append additional data points such as congressional districts, census codes, timezone, ACS survey results, UK constituencies and wards, and much much more.

To request additional fields, simply supply them as an array as the second parameter

$response = $geocoder->geocode(
[
'1109 N Highland St, Arlington VA',
'525 University Ave, Toronto, ON, Canada'
],
[
'cd',
'timezone'
]
);
$response = $geocoder->reverse('38.9002898,-76.9990361', ['census2010']);
// United Kingdom addresses support UK-specific appends such as Westminster and// devolved parliament constituencies, and local authority wards$response = $geocoder->geocode(
'10 Downing St, London, United Kingdom',
['uk-westminster', 'uk-local']
);

Address components

For forward geocoding requests it is possible to supply individual address components instead of a full address string. This works for both single and batch geocoding requests.

$response = $geocoder->geocode([
'street' => '1109 N Highland St',
'city' => 'Arlington',
'state_province' => 'VA',
'postal_code' => '22201'
]);
$response = $geocoder->geocode([
[
'street' => '1109 N Highland St',
'city' => 'Arlington',
'state_province' => 'VA'
],
[
'street' => '525 University Ave',
'city' => 'Toronto',
'state_province' => 'ON',
'country' => 'Canada',
],
[
'street' => '10 Downing St',
'city' => 'London',
'postal_code' => 'SW1A 2AA',
'country' => 'United Kingdom',
],
]);

Limit results

Optionally limit the number of maximum geocoding results by using the third parameter on geocode(...) or reverse(...)

$response = $geocoder->geocode('1109 N Highland St, Arlington, VA', [], 1); // Only get the first result$response = $geocoder->reverse('38.9002898,-76.9990361', ['timezone'], 5); // Return up to 5 geocoding results

Uploading Lists

The lists API lets you upload and process spreadsheet with addresses or coordinates. Similar to the spreadsheet feature in the dashboard, the spreadsheet will be processed as a job on Geocodio's infrastructure and can be downloaded at a later time. While a spreadsheet is being processed it is possible to query the status and progress.

Important

Data for spreadsheets processed through the lists API are automatically deleted 72 hours after they have finished processing. In addition to a 1GB file size limit, we recommend a maximum of 10M lookups per list batch. Larger batches should be split up into multiple list jobs.

See the API docs for geocoding lists for additional details.

Upload list from a file

Creates a new spreadsheet list job and starts processing the list in the background. The response returns a list id that can be used to retrieve the job progress as well as download the processed list when it has completed.

$response = $geocoder->uploadList(
file: 'path/to/file.csv',
direction: GeocodeDirection::Forward,
format: '{{B}} {{C}} {{D}} {{E}}',
callbackWebhook: 'https://example.com/callbacks/list-upload',
);
/*array:2 [ "id" => 11953719 "file" => array:3 [ "headers" => array:5 [ 0 => "Name" 1 => "Address" 2 => "City" 3 => "State" 4 => "Zip" ] "estimated_rows_count" => 4 "filename" => "simple.csv" ]]*/

Upload list of inline data

$csvData = <<<'CSV'name,street,city,state,zip"Peregrine Espresso","660 Pennsylvania Ave SE",Washington,DC,20003"Lot 38 Espresso Bar","1001 2nd St SE",Washington,DC,20003
CSV;
$geocodio->uploadInlineList(
$csvData,
'coffee-shops.csv',
GeocodeDirection::Forward,
'{{B}} {{C}} {{D}} {{E}}'
);

List status

View the metadata and status for a single uploaded list.

$geocoder->listStatus(11950669);
/*array:6 [ "id" => 11953719 "fields" => [] "file" => array:2 [ "estimated_rows_count" => 4 "filename" => "simple.csv" ] "status" => array:5 [ "state" => "COMPLETED" "progress" => 100 "message" => "Completed" "time_left_description" => null "time_left_seconds" => null ] "download_url" => "https://api.geocod.io/v2/lists/11953719/download" "expires_at" => "2025-08-22T20:36:10.000000Z"]*/

Download a list

Download a fully geocoded list, the returned format will always be a UTF-8 encoded, comma-separated csv file.

$geocoder->downloadList(11950669, 'path/to/file.csv');

Fetch all uploaded lists

Show all lists that have been created. The endpoint is paginated, showing 15 lists at a time, ordered by recency.

$geocoder->lists();
/*array:9 [ "current_page" => 1 "data" => array:1 [ 0 => array:6 [ "id" => 11953719 "fields" => [] "file" => array:2 [ "estimated_rows_count" => 4 "filename" => "simple.csv" ] "status" => array:5 [ "state" => "COMPLETED" "progress" => 100 "message" => "Completed" "time_left_description" => null "time_left_seconds" => null ] "download_url" => "https://api.geocod.io/v2/lists/11953719/download" "expires_at" => "2025-08-22T20:36:10.000000Z" ] "first_page_url" => "https://api.geocod.io/v2/lists?page=1" "from" => 1 "next_page_url" => null "path" => "https://api.geocod.io/v2/lists" "per_page" => 15 "prev_page_url" => null "to" => 3]*/

Delete uploaded list

Delete a previously uploaded list and its underlying spreadsheet data permanently. This can also be used to cancel and delete a spreadsheet that is currently processing.

Geocodio Unlimited customers can cancel a spreadsheet at any time. Pay as You Go customers can only cancel a spreadsheet if it was just recently started.

The spreadsheet data will always be deleted automatically after 72 hours if it is not deleted manually first.

$geocoder->deleteList(11950669);

Distance calculations

Calculate distances from a single origin to multiple destinations, or compute full distance matrices.

Coordinate format with custom IDs

You can add custom identifiers to coordinates using the lat,lng,id format. The ID will be returned in the response, making it easy to match results back to your data:

// String format with ID'37.7749,-122.4194,warehouse_1'// Array format with ID
[37.7749, -122.4194, 'warehouse_1']
// The ID is returned in the response:// [// 'query' => '37.7749,-122.4194,warehouse_1',// 'location' => [37.7749, -122.4194],// 'id' => 'warehouse_1',// 'distance_miles' => 3.2,// 'distance_km' => 5.1// ]

Distance mode and units

The SDK provides enums for type-safe distance configuration:

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Available modes
DistanceMode::Straightline // Default - great-circle (as the crow flies)
DistanceMode::Driving // Road network routing with duration// Available units
DistanceUnits::Miles // Default
DistanceUnits::Km
// Sorting options
DistanceOrderBy::Distance // Default
DistanceOrderBy::Duration
DistanceSortOrder::Asc // Default
DistanceSortOrder::Desc

Note: The default mode is straightline (great-circle distance). Use DistanceMode::Driving if you need road network routing with duration estimates.

Add distance to geocoding requests

You can add distance calculations to existing geocode or reverse geocode requests. Each geocoded result will include a destinations array with distances to each destination.

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Geocode an address and calculate distances to store locations$response = $geocoder->geocode(
'1600 Pennsylvania Ave NW, Washington DC',
destinations: [
'38.9072,-77.0369,store_dc',
'39.2904,-76.6122,store_baltimore',
'39.9526,-75.1652,store_philly'
],
distanceMode: DistanceMode::Driving,
distanceUnits: DistanceUnits::Miles
);
/*Response includes destinations for each geocoded result:[ 'results' => [ [ 'formatted_address' => '1600 Pennsylvania Ave NW, Washington, DC 20500', 'location' => ['lat' => 38.8977, 'lng' => -77.0365], 'destinations' => [ [ 'query' => '38.9072,-77.0369,store_dc', 'location' => [38.9072, -77.0369], 'id' => 'store_dc', 'distance_miles' => 0.8, 'distance_km' => 1.3, 'duration_seconds' => 180 ], [ 'query' => '39.2904,-76.6122,store_baltimore', 'location' => [39.2904, -76.6122], 'id' => 'store_baltimore', 'distance_miles' => 38.2, 'distance_km' => 61.5, 'duration_seconds' => 2820 ], // ... ] ] ]]*/// Reverse geocode with distances$response = $geocoder->reverse(
query: '38.8977,-77.0365',
destinations: [
'38.9072,-77.0369,capitol',
'38.8895,-77.0353,monument'
],
distanceMode: DistanceMode::Straightline
);
// With filtering - find nearest 3 stores within 50 miles$response = $geocoder->geocode(
'1600 Pennsylvania Ave NW, Washington DC',
destinations: [
'38.9072,-77.0369,store_1',
'39.2904,-76.6122,store_2',
'39.9526,-75.1652,store_3',
'40.7128,-74.0060,store_4'
],
distanceMode: DistanceMode::Driving,
distanceMaxResults: 3,
distanceMaxDistance: 50.0,
distanceOrderBy: DistanceOrderBy::Distance,
distanceSortOrder: DistanceSortOrder::Asc
);

Single origin to multiple destinations

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Calculate distances from one origin to multiple destinations$response = $geocoder->distance(
'37.7749,-122.4194,headquarters', // Origin with ID
[
'37.7849,-122.4094,customer_a',
'37.7949,-122.3994,customer_b',
'37.8049,-122.4294,customer_c'
]
);
/*Response:[ 'origin' => [ 'query' => '37.7749,-122.4194,headquarters', 'location' => [37.7749, -122.4194], 'id' => 'headquarters' ], 'destinations' => [ [ 'query' => '37.7849,-122.4094,customer_a', 'location' => [37.7849, -122.4094], 'id' => 'customer_a', 'distance_miles' => 0.9, 'distance_km' => 1.4 ], // ... ]]*/// Use driving mode for road network routing (includes duration)$response = $geocoder->distance(
'37.7749,-122.4194',
['37.7849,-122.4094'],
mode: DistanceMode::Driving
);
// With all filtering and sorting options$response = $geocoder->distance(
origin: '37.7749,-122.4194,warehouse',
destinations: [
'37.7849,-122.4094,store_1',
'37.7949,-122.3994,store_2',
'37.8049,-122.4294,store_3'
],
mode: DistanceMode::Driving,
units: DistanceUnits::Km,
maxResults: 2,
maxDistance: 10.0,
orderBy: DistanceOrderBy::Distance,
sortOrder: DistanceSortOrder::Asc
);
// Array format for coordinates (with or without ID)$response = $geocoder->distance(
[37.7749, -122.4194], // Without ID
[[37.7849, -122.4094, 'dest_1']] // With ID as third element
);

Distance matrix (multiple origins × destinations)

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Calculate full distance matrix with custom IDs$response = $geocoder->distanceMatrix(
origins: [
'37.7749,-122.4194,warehouse_sf',
'37.8049,-122.4294,warehouse_oak'
],
destinations: [
'37.7849,-122.4094,customer_1',
'37.7949,-122.3994,customer_2'
]
);
/*Response structure:[ 'results' => [ [ 'origin' => [ 'query' => '37.7749,-122.4194,warehouse_sf', 'location' => [37.7749, -122.4194], 'id' => 'warehouse_sf' ], 'destinations' => [ [ 'query' => '37.7849,-122.4094,customer_1', 'location' => [37.7849, -122.4094], 'id' => 'customer_1', 'distance_miles' => 0.9, 'distance_km' => 1.4 ], // ... ] ], [ 'origin' => [..., 'id' => 'warehouse_oak'], 'destinations' => [...] ] ]]*/// With driving mode and kilometers$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: ['37.7849,-122.4094'],
mode: DistanceMode::Driving,
units: DistanceUnits::Km
);

Nearest mode (find closest destinations)

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Find up to 2 nearest destinations from each origin$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: ['37.7849,-122.4094', '37.7949,-122.3994', '37.8049,-122.4294'],
maxResults: 2
);
// Filter by maximum distance (in miles or km depending on units)$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: [...],
maxDistance: 2.0
);
// Filter by minimum and maximum distance$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: [...],
minDistance: 1.0,
maxDistance: 10.0
);
// Filter by duration (seconds, driving mode only)$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: [...],
mode: DistanceMode::Driving,
maxDuration: 300, // 5 minutes
minDuration: 60// 1 minute minimum
);
// Sort by duration descending$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: [...],
mode: DistanceMode::Driving,
maxResults: 5,
orderBy: DistanceOrderBy::Duration,
sortOrder: DistanceSortOrder::Desc
);

Async Distance Matrix Jobs

For large distance matrix calculations, use async jobs that process in the background.

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
// Create a new distance matrix job$job = $geocoder->createDistanceMatrixJob(
name: 'My Distance Calculation',
origins: ['37.7749,-122.4194', '37.8049,-122.4294'],
destinations: ['37.7849,-122.4094', '37.7949,-122.3994'],
mode: DistanceMode::Driving,
units: DistanceUnits::Miles,
callbackUrl: 'https://example.com/webhook'// Optional
);
// Or use list IDs from previously uploaded lists$job = $geocoder->createDistanceMatrixJob(
name: 'Distance from List',
origins: 12345, // List ID
destinations: 67890, // List ID
mode: DistanceMode::Straightline
);
echo$job['id']; // Job identifier// Check job status$status = $geocoder->distanceMatrixJobStatus($job['id']);
/*[ 'id' => 'abc123', 'name' => 'My Distance Calculation', 'status' => [ 'state' => 'COMPLETED', // or 'PROCESSING', 'FAILED' 'progress' => 100, 'message' => 'Completed' ], 'download_url' => 'https://api.geocod.io/v2/distance-matrix/abc123/download', 'expires_at' => '2025-01-15T12:00:00.000000Z']*/// List all jobs$jobs = $geocoder->distanceMatrixJobs();
$jobs = $geocoder->distanceMatrixJobs(page: 2); // Paginated// Get job results as parsed array (same format as distance POST)$results = $geocoder->getDistanceMatrixJobResults($job['id']);
/*[ 'results' => [ [ 'origin' => ['query' => '...', 'location' => [...], 'id' => '...'], 'destinations' => [ ['query' => '...', 'location' => [...], 'id' => '...', 'distance_miles' => 1.2, ...] ] ], // ... ]]*/// Or download to file for very large results$geocoder->downloadDistanceMatrixJob($job['id'], '/path/to/results.json');
// Delete a job$geocoder->deleteDistanceMatrixJob($job['id']);

Note: Distance lookups are billed separately, please refer to https://www.geocod.io/pricing/ for more information.

Usage with Laravel

This library works well without Laravel, but if you happen to be using Laravel you can enjoy a few Laravel-specific features.

The package will be auto-discovered by newer Laravel versions, so the only thing left to do is to publish the config file

php artisan vendor:publish --provider="Geocodio\GeocodioServiceProvider"

You can now go ahead and edit your config file at config/geocodio.php.

You will now be able to use the Geocodio facade, or dependency inject the fully-configured Geocodio class.

// Using facadeuseGeocodio;
$response = Geocodio::geocode('1109 N Highland St, Arlington, VA');
// Using dependency injectionuseGeocodio\Geocodio;
class SomeController {
publicfunction__construct(Geocodio$geocoder) {
$response = $geocoder->geocode('1109 N Highland St, Arlington, VA');
}
}

Testing

$ composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Security

If you discover any security related issues, please email security@geocod.io instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.

About

geocod.io PHP library

Resources

Stars

106 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Repository files navigation

geocod.io PHP library Latest VersionTotal Downloads

Library for performing forward and reverse address geocoding for addresses or coordinates in the US, Canada, Mexico and the UK.

Installation

You can install the package via composer:

composer require geocodio/geocodio-library-php

Using Laravel? Great! There's an optional Laravel service provider, for easy integration into your app.

Usage

Don't have an API key yet? Sign up at https://dash.geocod.io to get an API key. The first 2,500 lookups per day are free.

Single geocoding

Using the Laravel integration? Check out Laravel-specific usage examples below.

$geocoder = newGeocodio\Geocodio();
$geocoder->setApiKey('YOUR_API_KEY');
// $geocoder->setHostname('api-hipaa.geocod.io'); // optionally overwrite the API hostname$response = $geocoder->geocode('1109 N Highland St, Arlington, VA');
dump($response);
/*array:1 [ "results" => array:1 [ 0 => array:6 [ "address_components" => array:10 [ "number" => "1109" "predirectional" => "N" "street" => "Highland" "suffix" => "St" "formatted_street" => "N Highland St" "city" => "Arlington" "county" => "Arlington County" "state_province" => "VA" "postal_code" => "22201" "country" => "US" ] "formatted_address" => "1109 N Highland St, Arlington, VA 22201" "location" => array:2 [ "lat" => 38.886672 "lng" => -77.094735 ] "accuracy" => 1 "accuracy_type" => "rooftop" "source" => "Arlington" ] ]]*/$response = $geocoder->reverse('38.9002898,-76.9990361');
$response = $geocoder->reverse([38.9002898, -76.9990361]);

Note: You can read more about accuracy scores, accuracy types, input formats and more at https://www.geocod.io/docs/

Batch geocoding

To batch geocode, simply pass an array of addresses or coordinates instead of a single string

$response = $geocoder->geocode([
'1109 N Highland St, Arlington VA',
'525 University Ave, Toronto, ON, Canada',
'10 Downing St, London, United Kingdom',
'4410 S Highway 17 92, Casselberry FL',
'15000 NE 24th Street, Redmond WA',
'17015 Walnut Grove Drive, Morgan Hill CA'
]);
$response = $geocoder->reverse([
'35.9746000,-77.9658000',
'32.8793700,-96.6303900',
'33.8337100,-117.8362320',
'35.4171240,-80.6784760'
]);
// Optionally supply a custom key that will be returned along with results$response = $geocoder->geocode([
'MyId1' => '1109 N Highland St, Arlington VA',
'MyId2' => '525 University Ave, Toronto, ON, Canada',
'MyId3' => '10 Downing St, London, United Kingdom',
'MyId4' => '4410 S Highway 17 92, Casselberry FL',
'MyId5' => '15000 NE 24th Street, Redmond WA',
'MyId6' => '17015 Walnut Grove Drive, Morgan Hill CA'
]);

Field appends

Geocodio allows you to append additional data points such as congressional districts, census codes, timezone, ACS survey results, UK constituencies and wards, and much much more.

To request additional fields, simply supply them as an array as the second parameter

$response = $geocoder->geocode(
[
'1109 N Highland St, Arlington VA',
'525 University Ave, Toronto, ON, Canada'
],
[
'cd',
'timezone'
]
);
$response = $geocoder->reverse('38.9002898,-76.9990361', ['census2010']);
// United Kingdom addresses support UK-specific appends such as Westminster and// devolved parliament constituencies, and local authority wards$response = $geocoder->geocode(
'10 Downing St, London, United Kingdom',
['uk-westminster', 'uk-local']
);

Address components

For forward geocoding requests it is possible to supply individual address components instead of a full address string. This works for both single and batch geocoding requests.

$response = $geocoder->geocode([
'street' => '1109 N Highland St',
'city' => 'Arlington',
'state_province' => 'VA',
'postal_code' => '22201'
]);
$response = $geocoder->geocode([
[
'street' => '1109 N Highland St',
'city' => 'Arlington',
'state_province' => 'VA'
],
[
'street' => '525 University Ave',
'city' => 'Toronto',
'state_province' => 'ON',
'country' => 'Canada',
],
[
'street' => '10 Downing St',
'city' => 'London',
'postal_code' => 'SW1A 2AA',
'country' => 'United Kingdom',
],
]);

Limit results

Optionally limit the number of maximum geocoding results by using the third parameter on geocode(...) or reverse(...)

$response = $geocoder->geocode('1109 N Highland St, Arlington, VA', [], 1); // Only get the first result$response = $geocoder->reverse('38.9002898,-76.9990361', ['timezone'], 5); // Return up to 5 geocoding results

Uploading Lists

The lists API lets you upload and process spreadsheet with addresses or coordinates. Similar to the spreadsheet feature in the dashboard, the spreadsheet will be processed as a job on Geocodio's infrastructure and can be downloaded at a later time. While a spreadsheet is being processed it is possible to query the status and progress.

Important

Data for spreadsheets processed through the lists API are automatically deleted 72 hours after they have finished processing. In addition to a 1GB file size limit, we recommend a maximum of 10M lookups per list batch. Larger batches should be split up into multiple list jobs.

See the API docs for geocoding lists for additional details.

Upload list from a file

Creates a new spreadsheet list job and starts processing the list in the background. The response returns a list id that can be used to retrieve the job progress as well as download the processed list when it has completed.

$response = $geocoder->uploadList(
file: 'path/to/file.csv',
direction: GeocodeDirection::Forward,
format: '{{B}} {{C}} {{D}} {{E}}',
callbackWebhook: 'https://example.com/callbacks/list-upload',
);
/*array:2 [ "id" => 11953719 "file" => array:3 [ "headers" => array:5 [ 0 => "Name" 1 => "Address" 2 => "City" 3 => "State" 4 => "Zip" ] "estimated_rows_count" => 4 "filename" => "simple.csv" ]]*/

Upload list of inline data

$csvData = <<<'CSV'name,street,city,state,zip"Peregrine Espresso","660 Pennsylvania Ave SE",Washington,DC,20003"Lot 38 Espresso Bar","1001 2nd St SE",Washington,DC,20003
CSV;
$geocodio->uploadInlineList(
$csvData,
'coffee-shops.csv',
GeocodeDirection::Forward,
'{{B}} {{C}} {{D}} {{E}}'
);

List status

View the metadata and status for a single uploaded list.

$geocoder->listStatus(11950669);
/*array:6 [ "id" => 11953719 "fields" => [] "file" => array:2 [ "estimated_rows_count" => 4 "filename" => "simple.csv" ] "status" => array:5 [ "state" => "COMPLETED" "progress" => 100 "message" => "Completed" "time_left_description" => null "time_left_seconds" => null ] "download_url" => "https://api.geocod.io/v2/lists/11953719/download" "expires_at" => "2025-08-22T20:36:10.000000Z"]*/

Download a list

Download a fully geocoded list, the returned format will always be a UTF-8 encoded, comma-separated csv file.

$geocoder->downloadList(11950669, 'path/to/file.csv');

Fetch all uploaded lists

Show all lists that have been created. The endpoint is paginated, showing 15 lists at a time, ordered by recency.

$geocoder->lists();
/*array:9 [ "current_page" => 1 "data" => array:1 [ 0 => array:6 [ "id" => 11953719 "fields" => [] "file" => array:2 [ "estimated_rows_count" => 4 "filename" => "simple.csv" ] "status" => array:5 [ "state" => "COMPLETED" "progress" => 100 "message" => "Completed" "time_left_description" => null "time_left_seconds" => null ] "download_url" => "https://api.geocod.io/v2/lists/11953719/download" "expires_at" => "2025-08-22T20:36:10.000000Z" ] "first_page_url" => "https://api.geocod.io/v2/lists?page=1" "from" => 1 "next_page_url" => null "path" => "https://api.geocod.io/v2/lists" "per_page" => 15 "prev_page_url" => null "to" => 3]*/

Delete uploaded list

Delete a previously uploaded list and its underlying spreadsheet data permanently. This can also be used to cancel and delete a spreadsheet that is currently processing.

Geocodio Unlimited customers can cancel a spreadsheet at any time. Pay as You Go customers can only cancel a spreadsheet if it was just recently started.

The spreadsheet data will always be deleted automatically after 72 hours if it is not deleted manually first.

$geocoder->deleteList(11950669);

Distance calculations

Calculate distances from a single origin to multiple destinations, or compute full distance matrices.

Coordinate format with custom IDs

You can add custom identifiers to coordinates using the lat,lng,id format. The ID will be returned in the response, making it easy to match results back to your data:

// String format with ID'37.7749,-122.4194,warehouse_1'// Array format with ID
[37.7749, -122.4194, 'warehouse_1']
// The ID is returned in the response:// [// 'query' => '37.7749,-122.4194,warehouse_1',// 'location' => [37.7749, -122.4194],// 'id' => 'warehouse_1',// 'distance_miles' => 3.2,// 'distance_km' => 5.1// ]

Distance mode and units

The SDK provides enums for type-safe distance configuration:

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Available modes
DistanceMode::Straightline // Default - great-circle (as the crow flies)
DistanceMode::Driving // Road network routing with duration// Available units
DistanceUnits::Miles // Default
DistanceUnits::Km
// Sorting options
DistanceOrderBy::Distance // Default
DistanceOrderBy::Duration
DistanceSortOrder::Asc // Default
DistanceSortOrder::Desc

Note: The default mode is straightline (great-circle distance). Use DistanceMode::Driving if you need road network routing with duration estimates.

Add distance to geocoding requests

You can add distance calculations to existing geocode or reverse geocode requests. Each geocoded result will include a destinations array with distances to each destination.

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Geocode an address and calculate distances to store locations$response = $geocoder->geocode(
'1600 Pennsylvania Ave NW, Washington DC',
destinations: [
'38.9072,-77.0369,store_dc',
'39.2904,-76.6122,store_baltimore',
'39.9526,-75.1652,store_philly'
],
distanceMode: DistanceMode::Driving,
distanceUnits: DistanceUnits::Miles
);
/*Response includes destinations for each geocoded result:[ 'results' => [ [ 'formatted_address' => '1600 Pennsylvania Ave NW, Washington, DC 20500', 'location' => ['lat' => 38.8977, 'lng' => -77.0365], 'destinations' => [ [ 'query' => '38.9072,-77.0369,store_dc', 'location' => [38.9072, -77.0369], 'id' => 'store_dc', 'distance_miles' => 0.8, 'distance_km' => 1.3, 'duration_seconds' => 180 ], [ 'query' => '39.2904,-76.6122,store_baltimore', 'location' => [39.2904, -76.6122], 'id' => 'store_baltimore', 'distance_miles' => 38.2, 'distance_km' => 61.5, 'duration_seconds' => 2820 ], // ... ] ] ]]*/// Reverse geocode with distances$response = $geocoder->reverse(
query: '38.8977,-77.0365',
destinations: [
'38.9072,-77.0369,capitol',
'38.8895,-77.0353,monument'
],
distanceMode: DistanceMode::Straightline
);
// With filtering - find nearest 3 stores within 50 miles$response = $geocoder->geocode(
'1600 Pennsylvania Ave NW, Washington DC',
destinations: [
'38.9072,-77.0369,store_1',
'39.2904,-76.6122,store_2',
'39.9526,-75.1652,store_3',
'40.7128,-74.0060,store_4'
],
distanceMode: DistanceMode::Driving,
distanceMaxResults: 3,
distanceMaxDistance: 50.0,
distanceOrderBy: DistanceOrderBy::Distance,
distanceSortOrder: DistanceSortOrder::Asc
);

Single origin to multiple destinations

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Calculate distances from one origin to multiple destinations$response = $geocoder->distance(
'37.7749,-122.4194,headquarters', // Origin with ID
[
'37.7849,-122.4094,customer_a',
'37.7949,-122.3994,customer_b',
'37.8049,-122.4294,customer_c'
]
);
/*Response:[ 'origin' => [ 'query' => '37.7749,-122.4194,headquarters', 'location' => [37.7749, -122.4194], 'id' => 'headquarters' ], 'destinations' => [ [ 'query' => '37.7849,-122.4094,customer_a', 'location' => [37.7849, -122.4094], 'id' => 'customer_a', 'distance_miles' => 0.9, 'distance_km' => 1.4 ], // ... ]]*/// Use driving mode for road network routing (includes duration)$response = $geocoder->distance(
'37.7749,-122.4194',
['37.7849,-122.4094'],
mode: DistanceMode::Driving
);
// With all filtering and sorting options$response = $geocoder->distance(
origin: '37.7749,-122.4194,warehouse',
destinations: [
'37.7849,-122.4094,store_1',
'37.7949,-122.3994,store_2',
'37.8049,-122.4294,store_3'
],
mode: DistanceMode::Driving,
units: DistanceUnits::Km,
maxResults: 2,
maxDistance: 10.0,
orderBy: DistanceOrderBy::Distance,
sortOrder: DistanceSortOrder::Asc
);
// Array format for coordinates (with or without ID)$response = $geocoder->distance(
[37.7749, -122.4194], // Without ID
[[37.7849, -122.4094, 'dest_1']] // With ID as third element
);

Distance matrix (multiple origins × destinations)

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Calculate full distance matrix with custom IDs$response = $geocoder->distanceMatrix(
origins: [
'37.7749,-122.4194,warehouse_sf',
'37.8049,-122.4294,warehouse_oak'
],
destinations: [
'37.7849,-122.4094,customer_1',
'37.7949,-122.3994,customer_2'
]
);
/*Response structure:[ 'results' => [ [ 'origin' => [ 'query' => '37.7749,-122.4194,warehouse_sf', 'location' => [37.7749, -122.4194], 'id' => 'warehouse_sf' ], 'destinations' => [ [ 'query' => '37.7849,-122.4094,customer_1', 'location' => [37.7849, -122.4094], 'id' => 'customer_1', 'distance_miles' => 0.9, 'distance_km' => 1.4 ], // ... ] ], [ 'origin' => [..., 'id' => 'warehouse_oak'], 'destinations' => [...] ] ]]*/// With driving mode and kilometers$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: ['37.7849,-122.4094'],
mode: DistanceMode::Driving,
units: DistanceUnits::Km
);

Nearest mode (find closest destinations)

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceOrderBy;
useGeocodio\Enums\DistanceSortOrder;
// Find up to 2 nearest destinations from each origin$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: ['37.7849,-122.4094', '37.7949,-122.3994', '37.8049,-122.4294'],
maxResults: 2
);
// Filter by maximum distance (in miles or km depending on units)$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: [...],
maxDistance: 2.0
);
// Filter by minimum and maximum distance$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: [...],
minDistance: 1.0,
maxDistance: 10.0
);
// Filter by duration (seconds, driving mode only)$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: [...],
mode: DistanceMode::Driving,
maxDuration: 300, // 5 minutes
minDuration: 60// 1 minute minimum
);
// Sort by duration descending$response = $geocoder->distanceMatrix(
origins: ['37.7749,-122.4194'],
destinations: [...],
mode: DistanceMode::Driving,
maxResults: 5,
orderBy: DistanceOrderBy::Duration,
sortOrder: DistanceSortOrder::Desc
);

Async Distance Matrix Jobs

For large distance matrix calculations, use async jobs that process in the background.

useGeocodio\Enums\DistanceMode;
useGeocodio\Enums\DistanceUnits;
// Create a new distance matrix job$job = $geocoder->createDistanceMatrixJob(
name: 'My Distance Calculation',
origins: ['37.7749,-122.4194', '37.8049,-122.4294'],
destinations: ['37.7849,-122.4094', '37.7949,-122.3994'],
mode: DistanceMode::Driving,
units: DistanceUnits::Miles,
callbackUrl: 'https://example.com/webhook'// Optional
);
// Or use list IDs from previously uploaded lists$job = $geocoder->createDistanceMatrixJob(
name: 'Distance from List',
origins: 12345, // List ID
destinations: 67890, // List ID
mode: DistanceMode::Straightline
);
echo$job['id']; // Job identifier// Check job status$status = $geocoder->distanceMatrixJobStatus($job['id']);
/*[ 'id' => 'abc123', 'name' => 'My Distance Calculation', 'status' => [ 'state' => 'COMPLETED', // or 'PROCESSING', 'FAILED' 'progress' => 100, 'message' => 'Completed' ], 'download_url' => 'https://api.geocod.io/v2/distance-matrix/abc123/download', 'expires_at' => '2025-01-15T12:00:00.000000Z']*/// List all jobs$jobs = $geocoder->distanceMatrixJobs();
$jobs = $geocoder->distanceMatrixJobs(page: 2); // Paginated// Get job results as parsed array (same format as distance POST)$results = $geocoder->getDistanceMatrixJobResults($job['id']);
/*[ 'results' => [ [ 'origin' => ['query' => '...', 'location' => [...], 'id' => '...'], 'destinations' => [ ['query' => '...', 'location' => [...], 'id' => '...', 'distance_miles' => 1.2, ...] ] ], // ... ]]*/// Or download to file for very large results$geocoder->downloadDistanceMatrixJob($job['id'], '/path/to/results.json');
// Delete a job$geocoder->deleteDistanceMatrixJob($job['id']);

Note: Distance lookups are billed separately, please refer to https://www.geocod.io/pricing/ for more information.

Usage with Laravel

This library works well without Laravel, but if you happen to be using Laravel you can enjoy a few Laravel-specific features.

The package will be auto-discovered by newer Laravel versions, so the only thing left to do is to publish the config file

php artisan vendor:publish --provider="Geocodio\GeocodioServiceProvider"

You can now go ahead and edit your config file at config/geocodio.php.

You will now be able to use the Geocodio facade, or dependency inject the fully-configured Geocodio class.

// Using facadeuseGeocodio;
$response = Geocodio::geocode('1109 N Highland St, Arlington, VA');
// Using dependency injectionuseGeocodio\Geocodio;
class SomeController {
publicfunction__construct(Geocodio$geocoder) {
$response = $geocoder->geocode('1109 N Highland St, Arlington, VA');
}
}

Testing

$ composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Security

If you discover any security related issues, please email security@geocod.io instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.

About

geocod.io PHP library

Resources

Stars

106 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages