How To Show Markers In Google Map Dynamically In Laravel

Hello artisan,

In this example, I will show you, how to show markers location in google map dynamically from database in laravel using latitude and longitude. I will fetch latitude and longitude and using them, I will generate a google map dynamically in Laravel 9 application.

From this laravel google map tutorial, you will learn how to pass latitude and longitude to google maps dynamically from server side and generate and showing markers dynamically.

From this tutorial, you will learn google map integration in laravel 9 application. If you don't know how to create google map with markers sign, then this tutorial is for you. So let's see the example tutorial of laravel google map marker generation process using latitude and longitude:

 

Step 1: Create Route

In this step, we need a route to show google map in Laravel application. So update web.php like below:

routes/web.php

use App\Models\Invoice;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::get('map',function(Request $request, Invoice $invoice){
    if($request->has('start_date')) {
        $data = $invoice->where('SRCode',$request->sr_code)
                        ->whereBetween('OrderDate',[
                            $request->start_date,
                            $request->end_date
                        ])
                        ->select('Latitude as lat','Longitude as lng')
                        ->get();
        return view('map',compact('data','request'));
    }
    return view('map');
})->name('google.map');

 

Step 2: Create View

Now in this step, we need a blade file for generating our google map by using our fetching data dynamically. Update map.blade.php like below:

resources/views/map.blade.php

 

Read also: Laravel 9 Dynamic Dependent Dropdown Using Ajax

 

Hope it can help you.

 

#laravel #laravel-9x #google-map