GeocoderLaravel reverse()返回null

时间:2018-08-01 20:26:46

标签: laravel laravel-5

我正在使用GeocoderLaravel package

Geocoder::geocode('5.43.168.58')->get()->first()->getCountry()工作正常。

但是,当我尝试:

Geocoder::reverse('52.7737667', '-108.2977717')->get()->first()

每次返回null

当我访问结果中找到的网址https://maps.googleapis.com/maps/api/geocode/json?latlng=52.773613,-108.297923&region=en-US&key=[MY API KEY]时,会得到以下正确的JSON响应:

{
   "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
               }
            },
......

我的config / geocoder.php文件:

    <?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,

    ];

我的.env

GOOGLE_MAPS_API_KEY=[MY API KEY HERE]
GOOGLE_MAPS_LOCALE=es

我尝试Geocoder::reverse('52.7737667', '-108.2977717')

时的JSON响应
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&region=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: []
  }
}

您可以看到#results: Collection为空

我尝试过app('geocoder')->reverse('52.7737667', '-108.2977717')->get()->first()

php artisan cache:clear

仍然返回一个空集合

如何解决?我想念什么吗?

编辑:

我刚刚尝试过重新安装laravel并获得一个空集合。

所以我觉得问题可能出在我的服务器上。这是我在做什么:

  1. Laravel新项目
  2. cd项目
  3. 撰写者需要toin0u / geocoder-laravel
  4. php artisan服务--port == 1010
  5. 在路由/web.php中的
  6. Route :: get('/',function(){

    dd(app('geocoder')->reverse('52.7737667', '-108.2977717'));
    

    });

这将返回:

ProviderAndDumperAggregator {#410 ▼
  #aggregator: ProviderAggregator {#411 ▶}
  #limit: null
  #results: Collection {#465 ▼
    #items: []
  }
}

0 个答案:

没有答案