0

Yii2 – Google Places API Library

Extension library to interact with Google Places API by wrapping the methods of our 2amigos/google-places-library into Yii2 components.

 

Installation

The preferred way to install this extension is through composer.

Either run

composer require 2amigos/google-places-library"

or add

"2amigos/google-places-library": "*"

to the require section of your composer.json file.

 

Usage

Once the extension is installed, simply use this code in view file :

<div class="box box-primary">

  <div class="box-body chart-responsive">
    
    <h2>Yii2 - Google Places API Library </h2>

    <div class="form-group">
        <input class="form-control" type="text" id="place" placeholder="Search Places">
    </div>
    
    <div id="output"></div>

    <?php
    $this->registerJs("
        $('#place').on('keyup', function() {
            var id = $(this).val();
            $.ajax({
                'type' : 'GET',
                'url' : '/yii2/web/index.php?r=google-place%2Fsearchplace',
                'dataType' : 'html',
                'data' : {
                    'name' : id
                },
                'success' : function(data){
                    $('#output').html(data);
                }
            });
        });
    ?>
    </div>

</div>

 

Add the class to your controller file.

use Da\Google\Places\Client\SearchClient;

 

In Controller, Now just create your search function to load into the view!

    public function actionSearchplace($name)
    {
    	$search = new SearchClient('API KEY');
        
        $results = $search->autoComplete($name);

        $results = json_decode(json_encode($results), true);

        if (is_object($results)) {
            $results= get_object_vars($results);
        }
        
        $datas = $results['predictions'];

        $html = '<ul>';
        foreach($datas as $data)
        {
            $html .= '<li>'.$data['description'].'</li>';
        }
        $html .= '</ul>';

        echo $html;
    }

 



Yii2 - Google Places API Library

Prakash S

Prakash S

I would like to introduce myself as a Software professional opting for the career in software industry. I'm Prakash S, a MCA graduate and trained as industry level practice for Software technology. Basically I am a PHP Developer but now days exploring more in HTML5, CSS, AngularJS and jQuery libraries.