本文介绍了通过Google Places API解析地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a huge(50k) db with addresses like

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

它们已使用Google Places Autocomplete js api自动完成,然后存储在数据库中.现在,我需要获取这些地址的不同部分statecityzip ...等.是否可以使用某些Google API?有什么想法吗?

They were autocompleted with google Places Autocomplete js api and then stored in db.Now I need to get distinct parts state, city, zip... etc of these addresses.Is it possible to do with some google of API?Any ideas?

推荐答案

我找到了解决方案.需要对Google Places API进行两次API调用.第一次调用通过完整地址获取PLACE_ID,第二次调用通过PLACE_ID获取有关地址的所有数据.

I found a solution.Need to do two API calls to google places API.First call to get PLACE_ID by full address, second one get all data about address by 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');
        }

这篇关于通过Google Places API解析地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 00:56