Google Autocomplete Address example in Laravel 6 – onlinecode

Google Autocomplete Address example in Laravel 6 – onlinecode

In this post we will give you information about Google Autocomplete Address example in Laravel 6 – onlinecode. Hear we will give you detail about Google Autocomplete Address example in Laravel 6 – onlinecodeAnd how to use it also give you demo for it if it is necessary.

In this article, we will discuss how to implement Google Autocomplete Address example in Laravel 6. The Google Autocomplete Address work based on latitude and the longitude.

here, we will use google API for Google Autocomplete Address in laravel 6. so you can follow the below steps.

Overview

Step 1: Install Laravel

Step 2: Get Google Place API Key

Step 3: Create Route

Step 4: Create a Controller

Step 5: Create Blade Files

Step 6: Run Our Laravel Application

Step 1: Install Laravel

We are going to install laravel 6, so first open the command prompt or terminal and go to go to xampp htdocs folder directory using the command prompt. after then run the below command.

PHP
composer create-project --prefer-dist laravel/laravel google_autocomplete_address

Step 2: Get Google Place API Key

Now, we have to need the Google Place API Key. so we will go to the cloud.google.com and after then login gets Google Place API.

Step 3: Create Route

Add the following route code in the “routes/web.php” file.

PHP
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
   // return view('welcome');	
});
Route::get('search-place','PlaceAutocompleteController@index');
?>

Step 4: Create a Controller

Here below command help to create the controller and model.

PHP
php artisan make:controller PlaceAutocompleteController

PlaceAutocompleteController.php

PHP
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use Response;
class PlaceAutocompleteController extends Controller
{
    public function index()
    {
       return view('autocomplete');   
    }
}
?>

Step 5: Create Blade Files

Finally, We will create an autocomplete.blade.php file in the “resources/views/” folder directory and paste the below code.

autocomplete.blade.php

PHP
<!doctype html>
<html lang="en">
  <head>
    <title>Google Autocomplete Address example in Laravel 6 - onlinecode</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="crossorigin="anonymous"></script>
  </head>
  <body>
    <div >
        <div >
            <div >
                <div >
                    <div >
                        <h5 >Google Autocomplete Address example in Laravel 6 - onlinecode</h5>
                    </div>
                    <div >
                        <div >
                            <label for="autocomplete"> Location/City/Address </label>
                            <input type="text" name="autocomplete" id="autocomplete"  placeholder="Select Location">
                        </div>
                        <div  id="lat_area">
                            <label for="latitude"> Latitude </label>
                            <input type="text" name="latitude" id="latitude" >
                        </div>

                        <div  id="long_area">
                            <label for="latitude"> Longitude </label>
                            <input type="text" name="longitude" id="longitude" >
                        </div>
                    </div>
                    <div >
                        <button type="submit" > Submit </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
{{-- javascript code --}}
<script src="https://maps.google.com/maps/api/js?key=Your_Google_Key=places&callback=initAutocomplete" type="text/javascript"></script>
<script>
   $(document).ready(function() {
        $("#lat_area").addClass("d-none");
        $("#long_area").addClass("d-none");
   });
</script>
<script>
   google.maps.event.addDomListener(window, 'load', initialize);
   function initialize() {
       var input = document.getElementById('autocomplete');
       var autocomplete = new google.maps.places.Autocomplete(input);
       autocomplete.addListener('place_changed', function() {
           var place = autocomplete.getPlace();
           $('#latitude').val(place.geometry['location'].lat());
           $('#longitude').val(place.geometry['location'].lng());
           $("#lat_area").removeClass("d-none");
           $("#long_area").removeClass("d-none");
       });
   }
</script>
  </body>
</html>

Step 6: Run Our Laravel ApplicationWe can start the server and run this example using the below command.

php artisan serve

Now we will run our example using the below Url in the browser.


Please follow and like us:

Hope this code and post will helped you for implement Google Autocomplete Address example in Laravel 6 – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  5  =  9

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US