PHP code example of geocodio / geocodio-library-php

1. Go to this page and download the library: Download geocodio/geocodio-library-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

geocodio / geocodio-library-php example snippets


$geocoder = new Geocodio\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);
/*
{
  "input": {
    "address_components": {
      "number": "1109"                                                       
      "predirectional": "N"                                                   
      "street": "Highland"                                                   
      "suffix": "St"                                                    
      "formatted_street": "N Highland St"                                     
      "city": "Arlington"                                                     
      "state": "VA"                                                           
      "country": "US"                                                         
    }                                                                        
    "formatted_address": "1109 N Highland St, Arlington, VA"                
  }             
  "results": array:2 [                                                         
    0 => {
      "address_components": {
        "number": "1109"                                                       
        "predirectional": "N"
        "street": "Highland"   
        "suffix": "St"
        "formatted_street": "N Highland St"
        "city": "Arlington"
        "county": "Arlington County"
        "state": "VA"
        "zip": "22201"
        "country": "US"
      }
      "formatted_address": "1109 N Highland St, Arlington, VA 22201"
      "location": {
        "lat": 38.886672
        "lng": -77.094735
      }
      "accuracy": 1
      "accuracy_type": "rooftop"
      "source": "Arlington"
    }
    1 => {
      "address_components": {
        "number": "1109"
        "predirectional": "N"
        "street": "Highland"
        "suffix": "St"
        "formatted_street": "N Highland St"
        "city": "Arlington"
        "county": "Arlington County"
        "state": "VA"
        "zip": "22201"
        "country": "US"
      }
      "formatted_address": "1109 N Highland St, Arlington, VA 22201"
      "location": {
        "lat": 38.886665
        "lng": -77.094733
      }
      "accuracy": 1
      "accuracy_type": "rooftop"
      "source": "Virginia Geographic Information Network (VGIN)"
    }
  ]
}
*/

$response = $geocoder->reverse('38.9002898,-76.9990361');
$response = $geocoder->reverse([38.9002898, -76.9990361]);

$response = $geocoder->geocode([
    '1109 N Highland St, Arlington VA',
    '525 University Ave, Toronto, ON, Canada',
    '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' => '4410 S Highway 17 92, Casselberry FL',
    'MyId4' => '15000 NE 24th Street, Redmond WA',
    'MyId5' => '17015 Walnut Grove Drive, Morgan Hill CA'
]);

$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']);

$response = $geocoder->geocode([
    'street' => '1109 N Highland St',
    'city' => 'Arlington',
    'state' => 'VA',
    'postal_code' => '22201'
]);

$response = $geocoder->geocode([
    [
        'street' => '1109 N Highland St',
        'city' => 'Arlington',
        'state' => 'VA'
    ],
    [
        'street' => '525 University Ave',
        'city' => 'Toronto',
        'state' => 'ON',
        'country' => 'Canada',
    ],
);

$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

// Using facade
use Geocodio;

$response = Geocodio::geocode('1109 N Highland St, Arlington, VA');

// Using dependency injection
use Geocodio\Geocodio;

class SomeController {
  public function __construct(Geocodio $geocoder) {
      $response = $geocoder->geocode('1109 N Highland St, Arlington, VA');
  }
}
bash
$ composer 

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