Skip to content

GregorVoelkl/geocoder

 
 

Repository files navigation

Geocode addresses to coordinates

Latest Version Software License Build Status SensioLabsInsight StyleCI Total Downloads

This PHP package can convert any address to GPS coordinates.

Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

Postcardware

You're free to use this package (it's MIT-licensed), but if it makes it to your production environment you are required to send us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

The best postcards will get published on the open source page on our website.

Installation

You can install this package through composer.

composer require spatie/geocoder

Laravel installation (OPTIONAL)

If you are using this package with Laravel, after installed it through composer, you must:

Install this service provider

// config/app.php
'providers' => [
    '...',
    'Spatie\Geocoder\GeocoderServiceProvider'
];

Geocoder also comes with a facade, which provides an easy way to call the Geocoder, so you have to register it

// config/app.php
'aliases' => array(
	...
	'Geocoder' => 'Spatie\Geocoder\GeocoderFacade',
)

Next, you must publish the config file :

php artisan vendor:publish --provider="Spatie\Geocoder\GeocoderServiceProvider" --tag="config"

This is the content of the config file:

return [

    /*
    |--------------------------------------------------------------------------
    | Google Maps API key
    |--------------------------------------------------------------------------
    |   You need to set the API key, which is required to send requests
    |   to Google's maps API
    |   More info: https://developers.google.com/maps/documentation/geocoding/intro#geocoding
    */

    'key' => env('GOOGLE_MAPS_GEOCODING_API_KEY', ''),

    /*
    |--------------------------------------------------------------------------
    | Language param [OPTIONAL]
    |--------------------------------------------------------------------------
    |   The language param used to set response translations for textual data
    |   (e.g. "formatted_address" field).
    |   More info: https://developers.google.com/maps/faq#languagesupport
    |
    */

    'language' => null,

    /*
    |--------------------------------------------------------------------------
    | Region param [OPTIONAL]
    |--------------------------------------------------------------------------
    |   The region param used to finetune the geocoding process.
    |   More info: https://developers.google.com/maps/documentation/geocoding/intro#RegionCodes
    |
    */

    'region' => null,

];

Usage

Here's how you can use the Geocoder

$geocoder = new Geocoder;
$geocoder->getCoordinatesForQuery('Infinite Loop 1, Cupertino', $apiKey);

/* 
  This function returns an array with keys
  "lat" =>  37.331741000000001
  "lng" => -122.0303329
  "accuracy" => "ROOFTOP"
  "formatted_address" => "1 Infinite Loop, Cupertino, CA 95014, USA"
*/

The language and region parameters are very useful in order to obtain the ù formatted_address` string (into the response), translated into the proper language (English is default), for example:

$language = 'it';
$region = 'it';

$geocoder = new Geocoder;
$geocoder->getCoordinatesForQuery('Infinite Loop 1, Cupertino', $apiKey, $language, $region);

/* 
  This function returns an array with keys
  "lat" =>  37.331741000000001
  "lng" => -122.0303329
  "accuracy" => "ROOFTOP"
  "formatted_address" => "1 Infinite Loop, Cupertino, CA 95014, Stati Uniti"
*/

If you are using the package with Laravel, you can simply call getCoordinatesForQuery on the facade, passing only the query parameter:

Geocoder::getCoordinatesForQuery('Infinite Loop 1, Cupertino');

/* 
  This function returns an array with keys
  "lat" =>  37.331741000000001
  "lng" => -122.0303329
  "accuracy" => "ROOFTOP"
  "formatted_address" => "1 Infinite Loop, Cupertino, CA 95014, Stati Uniti"
*/

The accuracy key can contain these values:

  • ROOFTOP
  • RANGE_INTERPOLATED
  • GEOMETRIC_CENTER
  • APPROXIMATE

You can read more information about these values on the Google Geocoding API Page

When an address is not found accuracy and formatted_address will contain NOT_FOUND

About Spatie

Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

About

Geocode addresses to coordinates

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%