Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 通过GooglePlacesAPI解析地址_Php_Google Maps_Google Api_Google Places Api - Fatal编程技术网

Php 通过GooglePlacesAPI解析地址

Php 通过GooglePlacesAPI解析地址,php,google-maps,google-api,google-places-api,Php,Google Maps,Google Api,Google Places Api,我有一个巨大的(50k)数据库,地址如下 12340 Via Moura, San Diego, CA, United States 17029 Avenida Cordillera, San Diego, CA, United States 3324 Sandleheath, Sarasota, FL 34235, USA 它们通过GooglePlacesAutoCompleteJSAPI自动完成,然后存储在数据库中。 现在我需要得到不同的部分state,city,zip。。。等这些地址。

我有一个巨大的(50k)数据库,地址如下

12340 Via Moura, San Diego, CA, United States
17029 Avenida Cordillera, San Diego, CA, United States
3324 Sandleheath, Sarasota, FL 34235, USA
它们通过GooglePlacesAutoCompleteJSAPI自动完成,然后存储在数据库中。 现在我需要得到不同的部分
state
city
zip
。。。等这些地址。 有没有可能用谷歌的API呢? 有什么想法吗?

我找到了解决办法。 需要对google places API执行两个API调用。 第一个调用通过完整地址获取PLACE_ID,第二个调用通过PLACE_ID获取关于地址的所有数据

        $params = [
            'key' => env('GOOGLE_API_KEY'),
            'query' => 'FULL_ADDRESS',
            'types' => 'address'
        ];
        $guzzle_client = new Client();
        $res = $guzzle_client->request('GET', 'https://maps.googleapis.com/maps/api/place/textsearch/json?parameters',
            [
                'query' => $params
            ]
        );

        $prediction = json_decode($res->getBody(), true);
        if ($prediction['status'] == 'OK') {
            $paramsID = [
                'key' => env('GOOGLE_API_KEY'),
                'placeid' => $prediction['results'][0]['place_id'],
            ];
            $guzzle_clientID = new Client();
            $resID = $guzzle_clientID->request('GET', 'https://maps.googleapis.com/maps/api/place/details/json',
                [
                    'query' => $paramsID
                ]
            );

            $predictionID = json_decode($resID->getBody(), true);

            $address_components = $predictionID['result']['address_components'];
            $address = [];
            foreach ($address_components as $component) {
                switch ($component['types'][0]) {
                    case 'street_number':
                        $address['street_number'] = $component['short_name'];
                        break;
                    case 'route':
                        $address['street_name'] = $component['short_name'];
                        break;
                    case 'locality':
                        $address['city'] = $component['short_name'];
                        break;
                    case 'administrative_area_level_1':
                        $address['state'] = $component['short_name'];
                        break;
                    case 'postal_code':
                        $address['zip'] = $component['short_name'];
                        break;
                }
            }
            $addresses[]= $address;
        } else {
            dd('Not found place');
        }