Uses PSR SimpleCache interface to allow for query results to be stored.
This is a standalone library, no Laravel or other Frameworks, to keep it as simple and lightweight as possible, but rest assured, it'll work with Laravel just fine :)
composer require perfacilis/geocoder
See: https://console.cloud.google.com/apis/credentials
Create a new API key, ensure to:
- restrict it to your IP(s) only;
- restrict it to use the Geocoding API only.
If you're using the Google Maps JS API you'll probably have to create a different key because that key should be restricted to HTTP referers (web sites).
$api_key = '123foo456bar';
$geocoder = new Perfacilis\Geocoder\Geocoder($api_key);
$result = $geocoder->geocode('Street 12, 1234AB, Residence, Country');
$lat = $result->getLat();
$lng = $result->getLng();
Since Google's Geocoding API is on a Pay-Per-Use basis, it's recommended to implement your own Cacher using PSR's SimpleCache interface:
$api_key = '123foo456bar';
$cacher = new GeocoderCache();
$geocoder = new Perfacilis\Geocoder\Geocoder($api_key);
$geocoder->setCacheInterface($cacher);
$result = $geocoder->query(...);
You can manually create a cacher to store results in a simple database:
class GeocoderCache implements Psr\SimpleCache\CacheInterface
{
...
}