- Notifications
You must be signed in to change notification settings - Fork105
Geocoder service provider for Laravel
License
geocoder-php/GeocoderLaravel
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
If you still useLaravel 4, please check out the
0.4.xbranchhere.
Version 4.0.0 is a backwards-compatibility-breaking update. Please reviewthis documentation, especially theUsage section before installing.
This package allows you to useGeocoderinLaravel 5.
- PHP >= 7.1.3
- Laravel >= 5.0
- Install the package via composer:
composer require toin0u/geocoder-laravel
- If you are running Laravel 5.5 (the package will be auto-discovered), skipthis step. Find the
providersarray key inconfig/app.phpand registertheGeocoder Service Provider:
// 'providers' => [Geocoder\Laravel\Providers\GeocoderService::class,// ];
- Optional I recommend adding the following lines to your
composer.jsonfile to prevent stale caches when upgrading or updating the package, both in your live and dev environments:
"post-update-cmd": ["@php artisan cache:clear", ],"post-install-cmd": ["@php artisan cache:clear", ]
Pay special attention to the language and region values if you are using them.For example, the GoogleMaps provider uses TLDs for region values, and thefollowing for language values:https://developers.google.com/maps/faq#languagesupport.
Further, a special note on the GoogleMaps provider: if you are using an API key,you must also use set HTTPS to true. (Best is to leave it true always, unlessthere is a special requirement not to.)
See theGeocoder documentation for a listof available adapters and providers.
To implement the dedicated cache store, add another redis store entry inconfig/database.php, something like the following:
"redis" => [// ..."geocode-cache" => [// choose an appropriate name'host' =>env('REDIS_HOST','192.168.10.10'),'password' =>env('REDIS_PASSWORD',null),'port' =>env('REDIS_PORT',6379),'database' =>1,// be sure this number differs from your other redis databases ], ]
You will also need to add an entry inconfig/cache.php to point to this redisdatabase:
"stores" => [// ..."geocode" => ['driver' =>'redis','connection' =>'geocode-cache', ], ],
Finally, configure Geocoder for Laravel to use this store. Editconfig/geocoder.php:
"cache" => ["store" =>"geocode",// ... ],
You can disable caching on a query-by-query basis as needed, like so:
$results =app("geocoder") ->doNotCache() ->geocode('Los Angeles, CA') ->get();
If you are upgrading and have previously published the geocoder config file, youneed to add thecache-duration variable, otherwise cache will be disabled(it will default to a0 cache duration). The default cache duration providedby the config file is999999999 seconds, essentially forever.
By default, the configuration specifies a Chain provider, containing theGoogleMaps provider for addresses as well as reverse lookups with lat/long,and the GeoIP provider for IP addresses. The first to return a resultwill be returned, and subsequent providers will not be executed. The defaultconfig file is kept lean with only those two providers.
However, you are free to add or remove providers as needed, both inside theChain provider, as well as along-side it. The following is the defaultconfiguration provided by the package:
useGeocoder\Provider\Chain\Chain;useGeocoder\Provider\GeoPlugin\GeoPlugin;useGeocoder\Provider\GoogleMaps\GoogleMaps;useHttp\Client\Curl\Client;return [/* |-------------------------------------------------------------------------- | Cache Duration |-------------------------------------------------------------------------- | | Specify the cache duration in seconds. The default approximates a forever | cache, but there are certain issues with Laravel's forever caching | methods that prevent us from using them in this project. | | Default: 9999999 (integer) | */'cache-duration' =>9999999,/* |-------------------------------------------------------------------------- | Providers |-------------------------------------------------------------------------- | | Here you may specify any number of providers that should be used to | perform geocaching operations. The `chain` provider is special, | in that it can contain multiple providers that will be run in | the sequence listed, should the previous provider fail. By | default the first provider listed will be used, but you | can explicitly call subsequently listed providers by | alias: `app('geocoder')->using('google_maps')`. | | Please consult the official Geocoder documentation for more info. | https://github.com/geocoder-php/Geocoder#providers | */'providers' => [ Chain::class => [ GoogleMaps::class => [env('GOOGLE_MAPS_LOCALE','us'),env('GOOGLE_MAPS_API_KEY'), ], GeoPlugin::class => [], ], ],/* |-------------------------------------------------------------------------- | Adapter |-------------------------------------------------------------------------- | | You can specify which PSR-7-compliant HTTP adapter you would like to use. | There are multiple options at your disposal: CURL, Guzzle, and others. | | Please consult the official Geocoder documentation for more info. | https://github.com/geocoder-php/Geocoder#usage | | Default: Client::class (FQCN for CURL adapter) | */'adapter' => Client::class,/* |-------------------------------------------------------------------------- | Reader |-------------------------------------------------------------------------- | | You can specify a reader for specific providers, like GeoIp2, which | connect to a local file-database. The reader should be set to an | instance of the required reader class or an array containing the reader | class and arguments. | | Please consult the official Geocoder documentation for more info. | https://github.com/geocoder-php/geoip2-provider | | Default: null | */'reader' =>null,];
By default we provide a CURL adapter to get you running out of the box.However, if you have already installed Guzzle or any other PSR-7-compatibleHTTP adapter, you are encouraged to replace the CURL adapter with it. Pleasesee theGeocoder Documentation forspecific implementation details.
If you would like to make changes to the default configuration, publish andedit the configuration file:
php artisan vendor:publish --provider="Geocoder\Laravel\Providers\GeocoderService" --tag="config"
The service provider initializes thegeocoder service, accessible via thefacadeGeocoder::... or the application helperapp('geocoder')->....
app('geocoder')->geocode('Los Angeles, CA')->get();
app('geocoder')->geocode('8.8.8.8')->get();
app('geocoder')->reverse(43.882587,-103.454067)->get();
app('geocoder')->geocode('Los Angeles, CA')->dump('kml');
useGeocoder\Laravel\ProviderAndDumperAggregatorasGeocoder;class GeocoderControllerextends Controller{publicfunctiongetGeocode(Geocoder$geocoder) {$geocoder->geocode('Los Angeles, CA')->get() }}
Anytime you upgrade this package, please remember to clear your cache, to prevent incompatible cached responses when breaking changes are introduced (this should hopefully only be necessary in major versions):
php artisan cache:clear
Update your composer.json file:
"toin0u/geocoder-laravel":"^4.0",
The one change to keep in mind here is that the results returned fromGeocoder for Laravel are now using the Laravel-native Collections classinstead of returning an instance ofAddressCollection. This should providegreater versatility in manipulation of the results, and be inline withexpectations for working with Laravel. The existingAddressCollectionmethods should map straight over to Laravel'sCollection methods. But be sureto double-check your results, if you have been usingcount(),first(),isEmpty(),slice(),has(),get(), orall() on your results.
Also,getProviders() now returns a Laravel Collection instead of an array.
Alert: if you have been using thegetIterator() method, it is no longerneeded. Simply iterate over your results as you would any other Laravelcollection.
Deprecated:
- the
all()method on the geocoder is being deprecated in favor of usingget(), which will return a Laravel Collection. You can then runall()on that. This method will be removed in version 5.0.0. - the
getProvider()method on the geocoder is being deprecated in favor of usinggetProviders(), which will return a Laravel Collection. You can then runfirst()on that to get the same result. This method will be removed in version 5.0.0.
Added: this version introduces a new way to create more complex queries:
- geocodeQuery()
- reverseQuery()
Please see theGeocoder documentationfor more details.
If you are upgrading from a pre-1.x version of this package, please keep thefollowing things in mind:
Update your composer.json file as follows:
"toin0u/geocoder-laravel":"^1.0",
Remove your
config/geocoder.phpconfiguration file. (If you need to customize it, follow the configuration instructions below.)Remove any Geocoder alias in the aliases section of your
config/app.php. (This package auto-registers the aliases.)Update the service provider entry in your
config/app.phpto read:Geocoder\Laravel\Providers\GeocoderService::class,
If you are using the facade in your code, you have two options:
Replace the facades
Geocoder::(and remove the correspondingusestatements) withapp('geocoder')->.Update the
usestatements to the following:useGeocoder\Laravel\Facades\Geocoder;
Update your query statements to use
->get()(to retrieve a collection ofGeoCoder objects) or->all()(to retrieve an array of arrays), then iterateto process each result.
- Clear cache:
php artisan cache:clear. - If you are still experiencing difficulties, please please open an issue on GitHub:https://github.com/geocoder-php/GeocoderLaravel/issues.
https://github.com/geocoder-php/GeocoderLaravel/blob/master/CHANGELOG.md
Please note that this project is released with aContributor Code of Conduct.By participating in this project you agree to abide by its terms.
GeocoderLaravel is released under the MIT License. See the bundledLICENSEfile for details.
About
Geocoder service provider for Laravel
Topics
Resources
License
Code of conduct
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.