1. Go to this page and download the library: Download spatie/geocoder 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/ */
return [
/*
* The api key used when sending Geocoding requests to Google.
*/
'key' => env('GOOGLE_MAPS_GEOCODING_API_KEY', ''),
/*
* The language param used to set response translations for textual data.
*
* More info: https://developers.google.com/maps/faq#languagesupport
*/
'language' => '',
/*
* The region param used to finetune the geocoding process.
*
* More info: https://developers.google.com/maps/documentation/geocoding/intro#RegionCodes
*/
'region' => '',
/*
* The bounds param used to finetune the geocoding process.
*
* More info: https://developers.google.com/maps/documentation/geocoding/intro#Viewports
*/
'bounds' => '',
/*
* The country param used to limit results to a specific country.
*
* More info: https://developers.google.com/maps/documentation/javascript/geocoding#GeocodingRequests
*/
'country' => '',
];
$client = new \GuzzleHttp\Client();
$geocoder = new Geocoder($client);
$geocoder->setApiKey(config('geocoder.key'));
$geocoder->setCountry(config('geocoder.country', 'US'));
$geocoder->getCoordinatesForAddress('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, USA",
"viewport" => [
"northeast" => [
"lat" => 37.3330546802915,
"lng" => -122.0294342197085
],
"southwest" => [
"lat" => 37.3303567197085,
"lng" => -122.0321321802915
]
]
*/
$geocoder->setLanguage('it');
$geocoder->getCoordinatesForAddress('Infinite Loop 1, Cupertino');
/*
This function returns an array with keys
"lat" => 37,3318598
"lng" => -122,0302485
"accuracy" => "ROOFTOP"
"formatted_address" => "Infinite Loop 1, 1 Infinite Loop, Cupertino, CA 95014, Stati Uniti"
...
*/