Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Google Map with Multiple Marker and Info Box in Laravel

Google Map with Multiple Marker and Info Box in Laravel

Aman Jain, August 30, 2022March 16, 2024

In this article we will learn to integrate google map with multiple marker and info box in laravel. Google maps are used to navigate the user location or indicate the location of business and any user. Sometimes In our application we want to show the multiple location with some information on map like to show nearby businesses, hotels, places etc. so in that case google maps are most optimal way to use map and marker. we will also learn to auto center the map while there is multiple marker in map.

There is many use cases of google maps with multiple marker and info box like show hotel price with info box , contact numbers, show near by places etc. and each has their own specif requirement to use in application.

In this tutorial we will use a simple example in which we show the google map and a marker of location is using latitude and longitude. this example will work in all versions of laravel including laravel 5, laravel 6, laravel 7, laravel 8 and laravel 9 too.

PREVIEW:

Google Map with Multiple Marker and Info Box in Laravel
Google Map with Multiple Marker and Info Box in Laravel

Let’s start the tutorial of integrate Google Map with Multiple Marker and Info Box in Laravel step by step

Step 1: Create a laravel project

First step is to create the Laravel project using composer command or you can also read the How to install laravel 8 ? and Laravel artisan command to generate controllers, Model, Components and Migrations

Step 2: Configure google map

Now configure the google map by creating the account on google developer account and get keys for map by following below steps

  1. Go to the Google Maps Platform > Credentials page.
  2. Go to the Credentials page
  3. On the Credentials page, click Create credentials > API key.
    The API key created dialog displays your newly created API key.
  4. Click Close.
    The new API key is listed on the Credentials page under API keys.

and then add it in environment file .env

GOOGLE_MAPS_API_KEY=KEY_HERE

Step 3: Create Controller

Next, Create the a controller and add the necessary imports and class. You can create by Laravel artisan or manually.

php artisan make:controller MapController 

Now, add the lat longs and pass it to view

    <?php

    namespace App\Http\Controllers;

    use App\Models\Article;
    use App\Models\User;
    use Illuminate\Http\Request;

    class MapController extends Controller
    {
         

        public function showMap(Request $request)
        {

          $locations = [
            ["lat" => 12.9716, "lng" => 77.5946],
            ["lat" => 26.9124, "lng" => 75.7873],
            ["lat" => 22.2587, "lng" => 71.1924],
          ];
          return view("show-map",['locations'=>$locations]);
        }
}

Step 4: Create View Files

Create a view file as we used in controller so create show-map.blade.php and add follow code

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Google Map with Multiple Marker and Info Box in Laravel - Readerstacks </title>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js" crossorigin="anonymous"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/jquery.validate.min.js"></script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" />

</head>

<body class="antialiased">
    <div class="container">
        <!-- main app container -->
        <div class="readersack">
            <div class="container">
                <div class="row">
                    <div class="col-md-12 ">
                        <h3>Google Map with Multiple Marker and Info Box in Laravel - Readerstacks</h3>
                        <div id="map" style='height:400px'></div>

                    </div>
                </div>
            </div>
        </div>
        <!-- credits -->
        <div class="text-center">
            <p>
                <a href="#" target="_top">Google Map with Multiple Marker and Info Box in Laravel
                </a>
            </p>
            <p>
                <a href="https://readerstacks.com" target="_top">readerstacks.com</a>
            </p>
        </div>
    </div>

</body>
<script type="text/javascript">
    function initializeMap() {
        const locations = <?php echo json_encode($locations) ?>;

        const map = new google.maps.Map(document.getElementById("map"));
        var infowindow = new google.maps.InfoWindow();
        var bounds = new google.maps.LatLngBounds();
        for (var location of locations) {
            var marker = new google.maps.Marker({
                position: new google.maps.LatLng(location.lat, location.lng),
                map: map
            });
            bounds.extend(marker.position);
            google.maps.event.addListener(marker, 'click', (function(marker, location) {
                return function() {
                    infowindow.setContent(location.lat + " & " + location.lng);
                    infowindow.open(map, marker);
                }
            })(marker, location));

        }
        map.fitBounds(bounds);
    }
</script>

<script type="text/javascript" src="https://maps.google.com/maps/api/js?key={{ env('GOOGLE_MAPS_API_KEY') }}&callback=initializeMap"></script>


</html>

main logic to add map is javascript which is here

 function initializeMap() {
        const locations = <?php echo json_encode($locations) ?>;

        const map = new google.maps.Map(document.getElementById("map"));
        var infowindow = new google.maps.InfoWindow();
        var bounds = new google.maps.LatLngBounds();
        for (var location of locations) {
            var marker = new google.maps.Marker({
                position: new google.maps.LatLng(location.lat, location.lng),
                map: map
            });
            bounds.extend(marker.position);
            google.maps.event.addListener(marker, 'click', (function(marker, location) {
                return function() {
                    infowindow.setContent(location.lat + " & " + location.lng);
                    infowindow.open(map, marker);
                }
            })(marker, location));

        }
        map.fitBounds(bounds);
    }

here we also added the auto center code so according to marker it will auto center the map

Step 5: Create the route

Final step is to create the route as follow

<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| 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('/show-map', "\App\Http\Controllers\ArticleController@showMap" );

Related

Php Laravel Laravel 9 googleinfoboxlaravelmapmultiple

Post navigation

Previous post
Next post

Related Posts

Php Laravel 9 Ajax CRUD with Search, Image and Pagination

Laravel 9 Ajax CRUD with Search, Image and Pagination

June 26, 2022March 28, 2023

In this article i will learn Ajax CRUD with Search, Image and Pagination in laravel 9. When we want to perform the CRUD operations without reloading the page then we use javascript and Ajax to refresh the page content without reloading.In this post we will not only cover the Ajax…

Read More
Php How to Create or Replace File Content in Laravel

How to Create or Replace File Content in Laravel ?

May 29, 2022November 9, 2023

This article will guide you to Check File exist and Create or Replace File Content in Laravel. To perform the file system based operations like Check file exist and create or replace file in laravel. we use File class or Storage class in laravel. Laravel provides inbuilt library to access…

Read More
Php Laravel password hash and check

How to create hash password in laravel 8 ?

January 26, 2022January 26, 2022

Laravel provides robust security features and one of them are hash password which is not decryptable. For hashing the password laravel use secure Bcrypt and Argon2 hashing for storing user passwords. Password encrypted with Bcrypt can not be decrypt since it uses key to generate the hashed string and irreversible…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version