简体   繁体   English

通过Google Places API解析地址

[英]Parse addresses through Google Places API

I have a huge(50k) db with addresses like 我有一个巨大的(50k)数据库,地址如下

12340 Via Moura, San Diego, CA, United States
17029 Avenida Cordillera, San Diego, CA, United States
3324 Sandleheath, Sarasota, FL 34235, USA

They were autocompleted with google Places Autocomplete js api and then stored in db. 它们是使用Google Places Autocomplete js api自动完成的,然后存储在数据库中。 Now I need to get distinct parts state , city , zip ... etc of these addresses. 现在,我需要获取这些地址的不同部分,包括statecityzip等。 Is it possible to do with some google of API? 是否可以使用某些Google API? Any ideas? 有任何想法吗?

I found a solution. 我找到了解决方案。 Need to do two API calls to google places API. 需要对Google Places API进行两次API调用。 First call to get PLACE_ID by full address, second one get all data about address by PLACE_ID. 第一次调用通过完整地址获取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');
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM