Reputation: 4783
I wanted to use this package for geocoding in Laravel. I have added it to providers and published the config, but I am getting trouble setting it up to work.
try {
$location = Geocoder::geocode('68.145.37.34')->get();
return $location;
} catch (\Exception $e) {
return $e;
}
This returns empty object.
I have left the config file as is.
return [
'cache-duration' => 9999999,
'providers' => [
Chain::class => [
GoogleMaps::class => [
'en-US',
env('GOOGLE_MAPS_API_KEY'),
],
GeoPlugin::class => [],
],
],
'adapter' => Client::class,
];
And added valid API key to env
. Is there something I'm missing?
Geocoder is imported as use Geocoder\Laravel\Facades\Geocoder;
EDIT
In case someone gets to the same problem...this is how you'd get the country from it:
app('geocoder')->geocode('5.43.168.58')->get()->first()->getCountry()->getName();
Really complicated unnecessarily in my opinion, I requested a documentation change on official repo.
Upvotes: 3
Views: 2153
Reputation: 189
did you try using dd() in tinker?? I have been try it...and it work for me..
try this :
dd(app('geocoder')->geocode('68.145.37.34')->get());
response :
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"
}
]
}
Upvotes: 2