I am using the GeocoderLaravel package
Geocoder::geocode('5.43.168.58')->get()->first()->getCountry()
works fine.
However when I try:
Geocoder::reverse('52.7737667', '-108.2977717')->get()->first()
It returns null
every time
When I visit the url https://maps.googleapis.com/maps/api/geocode/json?latlng=52.773613,-108.297923®ion=en-US&key=[MY API KEY]
that is found in the results, I get this correct JSON response:
{
"results" : [
{
"address_components" : [
{
"long_name" : "1101",
"short_name" : "1101",
"types" : [ "street_number" ]
},
{
"long_name" : "101 Street",
"short_name" : "101 St",
"types" : [ "route" ]
},
{
"long_name" : "North Battleford",
"short_name" : "North Battleford",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Division No. 16",
"short_name" : "Division No. 16",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Saskatchewan",
"short_name" : "SK",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Canada",
"short_name" : "CA",
"types" : [ "country", "political" ]
},
{
"long_name" : "S9A 0Z5",
"short_name" : "S9A 0Z5",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "1101 101 St, North Battleford, SK S9A 0Z5, Canada",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 52.7737667,
"lng" : -108.2977717
},
"southwest" : {
"lat" : 52.77352,
"lng" : -108.2981601
}
},
......
My config/geocoder.php file:
<?php
use Geocoder\Provider\Chain\Chain;
use Geocoder\Provider\GeoPlugin\GeoPlugin;
use Geocoder\Provider\GoogleMaps\GoogleMaps;
use Http\Client\Curl\Client;
return [
'cache' => [
/*
|-----------------------------------------------------------------------
| Cache Store
|-----------------------------------------------------------------------
|
| Specify the cache store to use for caching. The value "null" will use
| the default cache store specified in /config/cache.php file.
|
| Default: null
|
*/
'store' => null,
/*
|-----------------------------------------------------------------------
| Cache Duration
|-----------------------------------------------------------------------
|
| Specify the cache duration in minutes. 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)
|
*/
'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', 'en-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.
|
| Please consult the official Geocoder documentation for more info.
| https://github.com/geocoder-php/geoip2-provider
|
| Default: null
|
*/
'reader' => null,
];
My .env
GOOGLE_MAPS_API_KEY=[MY API KEY HERE]
GOOGLE_MAPS_LOCALE=es
The JSON response when I try Geocoder::reverse('52.7737667', '-108.2977717')
ProviderAndDumperAggregator {#954 ▼
#aggregator: ProviderAggregator {#955 ▼
-providers: array:1 [▼
"chain" => Chain {#982 ▼
-providers: array:2 [▼
"Geocoder\Provider\GoogleMaps\GoogleMaps" => GoogleMaps {#969 ▼
-region: "es"
-apiKey: "[MY API KEY]"
-clientId: null
-privateKey: null
-channel: null
-client: Client {#963 ▼
-options: []
-messageFactory: GuzzleMessageFactory {#965}
-streamFactory: GuzzleStreamFactory {#967}
-handle: curl resource @437 ▼
url: "https://maps.googleapis.com/maps/api/geocode/json?latlng=52.773613,-108.297923®ion=es&key={MY API KEY]"
content_type: null
http_code: 0
header_size: 0
request_size: 0
filetime: -1
ssl_verify_result: 20
redirect_count: 0
total_time: 0.297
namelookup_time: 1.0E-6
connect_time: 0.125
pretransfer_time: 0.0
size_upload: 0.0
size_download: 0.0
speed_download: 0.0
speed_upload: 0.0
download_content_length: -1.0
upload_content_length: -1.0
starttransfer_time: 0.0
redirect_time: 0.0
redirect_url: ""
primary_ip: "172.217.2.170"
certinfo: []
primary_port: 443
local_ip: "192.168.0.101"
local_port: 60896
}
-multiRunner: null
}
-messageFactory: GuzzleMessageFactory {#970}
}
"Geocoder\Provider\GeoPlugin\GeoPlugin" => GeoPlugin {#977 ▶}
]
#logger: null
}
]
-provider: null
-limit: 5
-decider: "Geocoder\ProviderAggregator::getProvider"
}
#limit: null
#results: Collection {#1025 ▼
#items: []
}
}
You can see #results: Collection
is empty
I have tried app('geocoder')->reverse('52.7737667', '-108.2977717')->get()->first()
php artisan cache:clear
Still returns an empty collection
How can this be fixed? Am I missing something?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire