2017-12-05 19 views
1

Laravelのジオコーディングにthis packageを使用したかったのですが、私はそれをプロバイダに追加してconfigを公開しましたが、うまく動作するように設定するのが面倒です。Laravel 5.5ジオコーダ

try { 
     $location = Geocoder::geocode('68.145.37.34')->get(); 
     return $location; 
    } catch (\Exception $e) { 
     return $e; 
    } 

これは空のオブジェクトを返します。

私は設定ファイルをそのまま残しました。

return [ 
    'cache-duration' => 9999999, 
    'providers' => [ 
     Chain::class => [ 
      GoogleMaps::class => [ 
       'en-US', 
       env('GOOGLE_MAPS_API_KEY'), 
      ], 
      GeoPlugin::class => [], 
     ], 
    ], 
    'adapter' => Client::class, 
]; 

有効なAPIキーをenvに追加しました。私は行方不明のものがありますか?ジオコーダは、誰かが同じ問題になる場合use Geocoder\Laravel\Facades\Geocoder;

EDIT

としてインポートされ

...これは、あなたがそれから国を取得したい方法です:

app('geocoder')->geocode('5.43.168.58')->get()->first()->getCountry()->getName(); 

私の意見では、本当に不必要に複雑で、私は公式レポの文書化の変更を要請しました。

+0

持っているあなたの他のIP意識ジオコーディングサービスのいずれかを試してみましたか? 'FreeGeoIp :: class'のように? –

+0

まあまあではありませんが、代替案を探しているわけではありません。 – Norgul

答えて

1

dd()を使ってみましたか?私は

はこれを試して...それを試してみてください...そして、それは私のために働くされています

dd(app('geocoder')->geocode('68.145.37.34')->get()); 

応答:

Illuminate\Support\Collection {#764 
    items: array:1 [ 
    0 => Geocoder\Model\Address {#753 
     -coordinates: Geocoder\Model\Coordinates {#755 
     -latitude: 51.0823 
     -longitude: -113.9578 
     } 
     -bounds: null 
     -streetNumber: null 
     -streetName: null 
     -subLocality: null 
     -locality: "Calgary" 
     -postalCode: null 
     -adminLevels: Geocoder\Model\AdminLevelCollection {#767 
     -adminLevels: array:1 [ 
      1 => Geocoder\Model\AdminLevel {#768 
      -level: 1 
      -name: "Alberta" 
      -code: "AB" 
      } 
     ] 
     } 
     -country: Geocoder\Model\Country {#769 
     -name: "Canada" 
     -code: "CA" 
     } 
     -timezone: null 
     -providedBy: "geo_plugin" 
    } 
    ] 
}