The package can convert any address to GPS coordinates.
You can install this package through Composer.
composer require spatie/geocoder
When using Laravel there is a service provider that you can make use of.
// app/config/app.php
'providers' => [
'...',
'Spatie\Geocoder\GeocoderServiceProvider'
];
Geocoder also comes with a facade, which provides an easy way to call the Geocoder.
// app/config/app.php
'aliases' => [
'...',
'Geocoder' => 'Spatie\Geocoder\GeocoderFacade',
];
Geocoder::getCoordinatesForQuery('Infinite Loop 1, Cupertino');
/*
This function returns an array with keys
"lat" => 37.331741000000001
"lng" => -122.0303329
"accuracy" => "ROOFTOP"
*/
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 will contain 'NOT_FOUND'