geocoder-php / GeocoderLaravel

Geocoder service provider for Laravel
http://geocoder-php.org/GeocoderLaravel/
MIT License
710 stars 102 forks source link

how to list all registered providers? #134

Closed wivaku closed 6 years ago

wivaku commented 6 years ago

How do I list the names of the registered providers? I do not want to manually + hardcoded create that list based, but dynamically based on actually registered providers. I tried using app('geocoder')->getProviders(). dd() shows them, but I do not seem to be able to access them. Using Laravel 5.6, geocoder-laravel v4.0.10.

When using dd() the chain of providers is shown.

dd( app('geocoder')->getProviders() );

Illuminate\Support\Collection {#641
  #items: array:1 [
    "chain" => Geocoder\Provider\Chain\Chain {#643
      -providers: array:4 [
        "Geocoder\Provider\GoogleMaps\GoogleMaps" => Geocoder\Provider\GoogleMaps\GoogleMaps {#649
          -region: "en-US"

When using foreach the chain itself is returned.

foreach (app('geocoder')->getProviders() as $provider) {
    echo "a: ". $provider->getName() ."\n";
}
// a: chain

When using dd() on ->first() I see the array of providers.

dd( app('geocoder')->getProviders()->first() );

Geocoder\Provider\Chain\Chain {#643
  -providers: array:4 [
    "Geocoder\Provider\GoogleMaps\GoogleMaps" => Geocoder\Provider\GoogleMaps\GoogleMaps {#649
      -region: "en-US"

When using foreach nothing is returned.

foreach (app('geocoder')->getProviders()->first() as $provider) {
    echo "b: ". $provider->getName() ."\n";
}
// empty

Background: trying to create a list like ['google_maps','opencage']. This so can iterate through the registered providers (e.g. if a certain provider gave an incomplete result, then use the next provider in the list to complement the previous result). Was planning to do something like this:

$result['google_maps'] = app('geocoder')->using('google_maps')->reverse($lat,$lon);
if (incomplete_results($result['google_maps'])) $result['next_provider'] = app('geocoder')->using('next_provider')->reverse($lat,$lon);
$result = combine_results($results);
mikebronner commented 6 years ago

The chain provider already does that -- it's purpose is to iterate through providers until it finds a result. This issue is specific to the chain provider, I would recommend opening this issue here: https://github.com/geocoder-php/Geocoder, but also do some code-diving to see how it works here: https://github.com/geocoder-php/chain-provider

Likely it isn't behaving as you need it to exactly, I would open an issue for that describes what you want it to do opposed to what it currently does.

(This repo is for the Laravel wrapper aspect only, I'm not directly involved with the core development.)