X
    Categories: JavascriptJqueryLaravel

Trigger google map marker from outside of map laravel

Trigger google map marker from outside of map laravel

In this tutorial we are going to see how to trigger google map marker from outside of map in laravel. When I was surfing ecommerce site, I have seen few of sites used google map to place based on product locations. On hover product they will trigger the map marker and open the pop up window with product details. Its looks cool and user freiendly too. The I have made few research about google map multiple markers and how to trigger google map from outside the map.

Lets see how to achieve this.

step 1 : Register google map key and use it in your script

Goto and register your unique key

Step 2 : Collect products locations in array

Step 3 : Call your locations in google map script and load in you map container

 

My map.blade.php

<div class="panel-body">

                  <div class="col-md-3">
                    <div class="col-md-12"  data-lat="40.59955" data-lng="-8.7498167" data-index="0" >
                        <img src="https://cdn.yourstory.com/wp-content/uploads/2016/08/125-fall-in-love.png">
                        <h4>Lat: -33.890542 , Long:151.274856 </h4>
                    </div>
                    <div class="col-md-12" data-lat="41.6386333" data-lng="-8.745" data-index="1" >
                        <img src="https://cdn2.hercampus.com/online%20shopping%20cover%20photo.jpg">
                        <h4>Lat: 41.6386333, Long: -8.745 </h4>
                    </div>
                    <div class="col-md-12" data-lat="-34.028249" data-lng="151.157507" data-index="2" >
                        <img src="http://technofaq.org/wp-content/uploads/2016/05/online-shopping-m.jpg">
                        <h4>Lat: -34.028249, Long: 151.157507</h4>
                    </div>
                    <div class="col-md-12" data-lat="-33.80010128657071" data-lng="151.28747820854187" data-index="3" >
                        <img src="http://www.thehindu.com/sci-tech/technology/internet/article17060632.ece/alternates/FREE_660/Users%20choice">
                        <h4>Lat: -33.80010128657071, Long: 151.28747820854187</h4>
                    </div>
                   
                  </div>

                  <div class="col-md-9">
                      <div id="map" style="width: 800px; height: 600px;"></div>
                  </div>

                </div>

My Script 
<script src="http://maps.google.com/maps/api/js?sensor=false&key=AIzaSyCcQtk4PMdtdQ"  type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
    var locations = [
        { 
          name : 'Coogee Beach', 
          image : 'https://cdn.yourstory.com/wp-content/uploads/2016/08/125-fall-in-love.png',
          content : 'Founded in 1824, the Porcelain Factory of Vista Alegre was the first industrial unit dedicated to porcelain production in Portugal. For the foundation and success of this risky industrial development was crucial the spirit of persistence of its founder. ',
          redirect_uri : 'http://www.17educations.com',
          lat : 40.59955, 
          lng : -8.7498167
        },
        { 
          name : 'Bondi Beach', 
          image : 'https://cdn2.hercampus.com/online%20shopping%20cover%20photo.jpg',
          content : 'Founded in 1824, the Porcelain Factory of Vista Alegre was the first industrial unit dedicated to porcelain production in Portugal. For the foundation and success of this risky industrial development was crucial the spirit of persistence of its founder. ',
          redirect_uri : 'http://www.17educations.com',
          lat : 41.6386333, 
          lng : -8.745 
        },

        { 
          name : 'Cronulla Beach', 
          image : 'http://technofaq.org/wp-content/uploads/2016/05/online-shopping-m.jpg',
          content : 'Founded in 1824, the Porcelain Factory of Vista Alegre was the first industrial unit dedicated to porcelain production in Portugal. For the foundation and success of this risky industrial development was crucial the spirit of persistence of its founder. ',
          redirect_uri : 'http://www.17educations.com',
          lat : -34.028249, 
          lng : 151.157507
        },
        { 
          name : 'Manly Beach', 
          image : 'http://www.thehindu.com/sci-tech/technology/internet/article17060632.ece/alternates/FREE_660/Users%20choice',
          content : 'Founded in 1824, the Porcelain Factory of Vista Alegre was the first industrial unit dedicated to porcelain production in Portugal. For the foundation and success of this risky industrial development was crucial the spirit of persistence of its founder. ',
          redirect_uri : 'http://www.17educations.com',
          lat : -33.80010128657071, 
          lng : 151.28747820854187 
        },
    ];
      var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 10,
        center: new google.maps.LatLng(locations[0].lat,  locations[0].lng ),
        mapTypeId: google.maps.MapTypeId.ROADMAP
      });

      var infowindow = new google.maps.InfoWindow();

      var marker, i;
      var markers = new Array();

      for (i = 0; i < locations.length; i++) {  
        marker = new google.maps.Marker({
          position: new google.maps.LatLng(locations[i].lat, locations[i].lng),
          map: map
        });

        
        google.maps.event.addListener(marker, 'mouseover', (function(marker, i) {
          return function() {
            var content = '<div id="iw-container">' +
                      '<div class="iw-title">'+locations[i].name+'</div>' +
                      '<div class="iw-content">' +
                        '<img src="'+locations[i].image+'" height="115" width="83">' +
                        '<p>'+locations[i].content+'</p>' +
                        '<p><a href="'+locations[i].redirect_uri+'">Click</a></p>' +
                      '</div>' +
                      '<div class="iw-bottom-gradient">$10</div>' +
                    '</div>';
            infowindow.setContent(content);
            infowindow.open(map, marker);
          }
        })(marker, i));

             /* // assuming you also want to hide the infowindow when user mouses-out
        marker.addListener('mouseout', function() {
            infowindow.close();
        });*/
        markers.push(marker); 
    }
 
  google.maps.event.addListener(map, 'click', function(event) {
    if (infowindow) {
            infowindow.close();
        };
  })
  google.maps.event.addListener(infowindow, 'domready', function() {

    // Reference to the DIV that wraps the bottom of infowindow
    var iwOuter = $('.gm-style-iw');
    /* Since this div is in a position prior to .gm-div style-iw.
     * We use jQuery and create a iwBackground variable,
     * and took advantage of the existing reference .gm-style-iw for the previous div with .prev().
    */
    var iwBackground = iwOuter.prev();
    // Removes background shadow DIV
    iwBackground.children(':nth-child(2)').css({'display' : 'none'});
    // Removes white background DIV
    iwBackground.children(':nth-child(4)').css({'display' : 'none'});
    // Moves the infowindow 115px to the right.
    iwOuter.parent().parent().css({left: '115px'});
    // Moves the shadow of the arrow 76px to the left margin.
    iwBackground.children(':nth-child(1)').attr('style', function(i,s){ return s + 'left: 76px !important;'});
    // Moves the arrow 76px to the left margin.
    iwBackground.children(':nth-child(3)').attr('style', function(i,s){ return s + 'left: 76px !important;'});
    // Changes the desired tail shadow color.
    iwBackground.children(':nth-child(3)').find('div').children().css({'box-shadow': 'rgba(72, 181, 233, 0.6) 0px 1px 6px', 'z-index' : '1'});
    // Reference to the div that groups the close button elements.
    var iwCloseBtn = iwOuter.next();
    // Apply the desired effect to the close button
    iwCloseBtn.css({opacity: '1', right: '38px', top: '3px', border: '7px solid #48b5e9', 'border-radius': '13px', 'box-shadow': '0 0 5px #3990B9'});
    // If the content of infowindow not exceed the set maximum height, then the gradient is removed.
    if($('.iw-content').height() < 140){
      $('.iw-bottom-gradient').css({display: 'none'});
    }
    // The API automatically applies 0.7 opacity to the button after the mouseout event. This function reverses this event to the desired value.
    iwCloseBtn.mouseout(function(){
      $(this).css({opacity: '1'});
    });
  });


  $('.col-md-3 .col-md-12').hover(
  function() {
     if($(this).attr('data-lat') != undefined && $(this).attr('data-lng') != undefined ) {
        LatLng =  { lat: parseInt($(this).attr('data-lat')) , lng: parseInt($(this).attr('data-lng')) };
        map.setCenter(LatLng);
        map.setZoom(10);
        google.maps.event.trigger(markers[$(this).attr('data-index')], 'mouseover');
      } else {
        if (infowindow) {
             infowindow.close();
         }
      }
  },
  function() {
  });

  $("#map_whole_products").hover(
  function () {
  },
  function () {
    if (infowindow) {
             infowindow.close();
    }
  }
);
  </script>

My css

#map_whole_products .col-md-12 img { width: 100%;  }

#map-canvas {
  margin: 0;
  padding: 0;
  height: 400px;
  max-width: none;
}
#map-canvas img {
  max-width: none !important;
}
.gm-style-iw {
  width: 350px !important;
  top: 15px !important;
  left: 0px !important;
  background-color: #fff;
  box-shadow: 0 1px 6px rgba(178, 178, 178, 0.6);
  border: 1px solid rgba(72, 181, 233, 0.6);
  border-radius: 2px 2px 10px 10px;
}
#iw-container {
  margin-bottom: 10px;
}
#iw-container .iw-title {
  font-family: 'Open Sans Condensed', sans-serif;
  font-size: 22px;
  font-weight: 400;
  padding: 10px;
  background-color: #48b5e9;
  color: white;
  margin: 0;
  border-radius: 2px 2px 0 0;
}
#iw-container .iw-content {
  font-size: 13px;
  line-height: 18px;
  font-weight: 400;
  margin-right: 1px;
  padding: 15px 5px 20px 15px;
  max-height: 140px;
  overflow-y: auto;
  overflow-x: hidden;
}
.iw-content img {
  float: right;
  margin: 0 5px 5px 10px; 
}
.iw-subTitle {
  font-size: 16px;
  font-weight: 700;
  padding: 5px 0;
}
.iw-bottom-gradient {
  position: absolute;
  width: 326px;
  height: 25px;
  bottom: 10px;
  right: 18px;
  background: linear-gradient(to bottom, rgba(255,255,255,0) 0%, rgba(255,255,255,1) 100%);
  background: -webkit-linear-gradient(top, rgba(255,255,255,0) 0%, rgba(255,255,255,1) 100%);
  background: -moz-linear-gradient(top, rgba(255,255,255,0) 0%, rgba(255,255,255,1) 100%);
  background: -ms-linear-gradient(top, rgba(255,255,255,0) 0%, rgba(255,255,255,1) 100%);
}

How to trigger Marker outside of map

In above I have added my javascript code to place a map
During the initialisation of marker I have created the custom array as markers and stored all markers in my custom array. It will store based on index value.

In the same way I have listed my products and given index in data attribute.

On hover product I will collect data attribute informations and trigger the map marker like below

    google.maps.event.trigger(markers[index], ‘click’); //This is the main function to trigger google map
$('.col-md-3 .col-md-12').hover(
  function() {
     if($(this).attr('data-lat') != undefined && $(this).attr('data-lng') != undefined ) {
        LatLng =  { lat: parseInt($(this).attr('data-lat')) , lng: parseInt($(this).attr('data-lng')) };
        map.setCenter(LatLng);
        map.setZoom(10);
        google.maps.event.trigger(markers[$(this).attr('data-index')], 'mouseover');
      } else {
        if (infowindow) {
             infowindow.close();
         }
      }
  },
  function() {
  });

 

Thats it. This is basic example hope it will help some to achieve something complex works.
Thanks!.

Marimuthu: