PHP code example of ride / lib-geocode

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

    

ride / lib-geocode example snippets




use ride\library\http\client\Client;
use ride\library\geocode\service\ArcgisGeocodeService;
use ride\library\geocode\service\ChainGeocodeService;
use ride\library\geocode\service\FreeGeoIpGeocodeService;
use ride\library\geocode\service\GoogleGeocodeService;
use ride\library\geocode\Geocoder;

function createGeocoder(Client $httpClient) {
    // create a google service
    $googleService = new GoogleGeocodeService($httpClient);
    // optionally set a API key
    $googleService->setApiKey('your-api-key');
    
    // create a chain of address services
    $addressService = new ChainGeocodeService('address');
    $addressService->addService($googleService);
    $addressService->addService(new ArcgisGeocodeService($httpClient)); 
    
    // create a chain of ip services
    $ipService = new ChainGeocodeService('ip');
    $ipService->addService(new FreeGeoIpGeocodeService($httpClient));
    
    // create the geocoder and set our defined services to it
    $geocoder = new Geocoder();
    $geocoder->addService($addressService);
    $geocoder->addService($ipService);
    
    return $geocoder;
}

function geocodeStuff(Geocoder $geocoder) {
    try {
        $geocodeResult = $geocoder->geocode('address', 'Vital de costerstraat, Leuven'); 
        $geocodeResult = $geocoder->geocode('ip', 'github.com');
        $geocodeResult = $geocoder->geocode('ip', '8.8.8.8');
    } catch (GeocodeException $exception) {
        // could not find any result
    }
}