Advertisement
Guest User

Google Maps

a guest
Mar 1st, 2017
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 8.65 KB | None | 0 0
  1. app.googlemaps = (function($){
  2.   'use strict';
  3.  
  4.     // global var
  5.     var map = null,
  6.         baseMapOptions = {};
  7.  
  8.  
  9.     // Asynchronously load google maps script with callback to load infobox and markerclusterer scripts
  10.     $(window).load(function() {
  11.         setTimeout(function() {
  12.             if ( $('.js-hood-map').length || $('.js-map').length || $('.js-property-search-map') ) {
  13.                 var script = document.createElement('script');
  14.                 script.src = 'https://maps.googleapis.com/maps/api/js?key=AIzaSyBB_9t84onBDSydB2CznoSqIN2jp0hFPcQ&callback=initAllMosaicMaps';
  15.                 document.getElementsByTagName('head')[0].appendChild(script);
  16.             }
  17.         },2000)
  18.     })
  19.  
  20.  
  21.     // Asynchronously load infobox and markerclusterer scripts
  22.     // Only after they have loaded, initiate maps
  23.     function initSequence() {
  24.         var script = document.createElement('script');
  25.         script.src = D7WP.templateUrl + '/src/js/vendor/LAB.min.js';
  26.         script.onload = function() {
  27.             $LAB
  28.                 .script(D7WP.templateUrl + '/src/js/vendor/infobox.js').wait()
  29.                 .script(D7WP.templateUrl + '/src/js/vendor/markerclusterer.js').wait(function(){
  30.                     // Initiate .js-hood-map and .js-map
  31.                     initAllMaps();
  32.                     // Initiate .js-property-search-map map
  33.                     app.propertySearch.bindMap();
  34.                 });
  35.         };
  36.         document.getElementsByTagName('head')[0].appendChild(script);
  37.     }
  38.  
  39.  
  40.     // Init all maps based on class
  41.     function initAllMaps(){
  42.  
  43.         //generic map
  44.         $('.js-map').each(function() {
  45.             map = newMap( $(this) );
  46.         });
  47.  
  48.     }
  49.  
  50.  
  51.   /*
  52.   *  newMap
  53.   *
  54.   *  This function will render a Google Map onto the selected jQuery element
  55.   */
  56.   function newMap( $el, mapOptions) {
  57.  
  58.     //baseOptions
  59.     baseMapOptions = {
  60.         // Base styles use 'Corporate' theme from Mosaic snazzy maps account
  61.         styles: [{'featureType':'all','elementType':'labels.text.fill','stylers':[{'color':'#585858'},{'visibility':'on'}]},{'featureType':'administrative','elementType':'labels.text.fill','stylers':[{'color':'#444444'}]},{'featureType':'administrative.locality','elementType':'labels.text.fill','stylers':[{'color':'#585858'}]},{'featureType':'administrative.neighborhood','elementType':'labels.text.fill','stylers':[{'color':'#585858'}]},{'featureType':'landscape','elementType':'all','stylers':[{'color':'#eeeeee'}]},{'featureType':'poi','elementType':'all','stylers':[{'visibility':'off'}]},{'featureType':'poi','elementType':'labels','stylers':[{'color':'#007da4'},{'visibility':'off'}]},{'featureType':'poi','elementType':'labels.text.fill','stylers':[{'color':'#585858'}]},{'featureType':'poi','elementType':'labels.text.stroke','stylers':[{'visibility':'off'},{'color':'#ffffff'}]},{'featureType':'poi.park','elementType':'geometry.fill','stylers':[{'color':'#d7d7d7'},{'visibility':'on'}]},{'featureType':'poi.park','elementType':'labels','stylers':[{'visibility':'on'}]},{'featureType':'road','elementType':'all','stylers':[{'saturation':-100},{'lightness':45}]},{'featureType':'road','elementType':'labels.text.fill','stylers':[{'color':'#585858'}]},{'featureType':'road.highway','elementType':'all','stylers':[{'visibility':'simplified'}]},{'featureType':'road.highway','elementType':'geometry.fill','stylers':[{'color':'#ffffff'}]},{'featureType':'road.arterial','elementType':'labels.icon','stylers':[{'visibility':'off'}]},{'featureType':'transit','elementType':'all','stylers':[{'visibility':'off'}]},{'featureType':'water','elementType':'all','stylers':[{'color':'#b4b4b4'},{'visibility':'on'}]}],
  62.         zoom: 16,
  63.         clickableIcons: false,
  64.         center: new google.maps.LatLng(0, 0),
  65.         mapTypeId: google.maps.MapTypeId.ROADMAP,
  66.         scrollwheel: false,
  67.     };
  68.  
  69.     // var
  70.     var $markers = $el.find('.marker');
  71.  
  72.     var options =  $.extend({}, baseMapOptions, mapOptions || {} );
  73.  
  74.     // create map
  75.     var map = new google.maps.Map( $el[0], options);
  76.  
  77.     // add a markers reference
  78.     map.markers = [];
  79.  
  80.     // map marker icons
  81.     var icons = {
  82.         counter: {
  83.             size: {
  84.                 width: '30',
  85.                 height: '41'
  86.             },
  87.             anchor: {
  88.                 x: '15',
  89.                 y: '41'
  90.             }
  91.         },
  92.         pin: {
  93.             size: {
  94.                 width: '39',
  95.                 height: '53'
  96.             },
  97.             anchor: {
  98.                 x: '19',
  99.                 y: '53'
  100.             }
  101.         }
  102.     };
  103.  
  104.     // add markers
  105.     $markers.each(function(){
  106.  
  107.         if( $(this).attr('data-label') === 'counter' ) {
  108.  
  109.             icons[$(this).attr('data-label')].url = D7WP.templateUrl + '/images/'+ $(this).attr('data-theme') +'-counter.png';
  110.  
  111.             add_marker( $(this), map, icons[$(this).attr('data-label')], 'small-pin' );
  112.  
  113.         } else {
  114.  
  115.             icons[$(this).attr('data-label')].url = D7WP.templateUrl + '/images/'+ $(this).attr('data-theme') +'-pin.png';
  116.  
  117.             add_marker( $(this), map, icons[$(this).attr('data-label')], 'large-pin' );
  118.  
  119.         }
  120.  
  121.     });
  122.  
  123.  
  124.     // center map
  125.     centerMap( map );
  126.  
  127.     return map;
  128.  
  129.   }
  130.  
  131.  
  132.   /*
  133.   *  add_marker
  134.   *
  135.   *  This function will add a marker to the selected Google Map
  136.   *
  137.   */
  138.  
  139.   function add_marker( $marker, map, icon, iconType ) {
  140.  
  141.     // var
  142.     var latlng = new google.maps.LatLng( $marker.attr('data-lat'), $marker.attr('data-lng') );
  143.  
  144.     if ( typeof icon !== 'undefined' ) {
  145.         var customIconImage = {
  146.             url: icon.url,
  147.             scaledSize: new google.maps.Size(icon.size.width, icon.size.height),
  148.             anchor: new google.maps.Point(icon.anchor.x, icon.anchor.y)
  149.         };
  150.     }
  151.  
  152.     // create marker
  153.     var marker = new google.maps.Marker({
  154.         position    : latlng,
  155.         map         : map,
  156.         icon        : customIconImage,
  157.         iconType    : iconType,
  158.         label       : '',
  159.         //Pass in data-attrs
  160.         data        : $marker.data(),
  161.     });
  162.  
  163.     // add to array
  164.     map.markers.push( marker );
  165.  
  166.     // if marker contains HTML, add it to an infoWindow
  167.     if( $marker.html() )
  168.     {
  169.         var boxClasses = ['infoBox'];
  170.  
  171.         //add theme class if present
  172.         if ( $marker.attr('data-theme') ) {
  173.           boxClasses.push($marker.attr('data-theme'));
  174.         }
  175.  
  176.         // If marker html contains data-style attribute, change to style attribute
  177.         if ( $('[data-style]',$marker).length > 0 ) {
  178.             var dataStyle = $('[data-style]',$marker).data('style');
  179.             $('[data-style]',$marker).attr('style',dataStyle);
  180.         }
  181.  
  182.         // create info window
  183.         // docs: https://github.com/googlemaps/v3-utility-library/blob/master/infobox/docs/reference.html
  184.         var infowindow = new InfoBox({
  185.             boxClass: boxClasses.join(' '),  //note infoBox is the default
  186.             closeBoxURL : '',
  187.             content     : $marker.html(),
  188.             pixelOffset: new google.maps.Size(-100, 20)
  189.         });
  190.  
  191.         // On marker click
  192.         google.maps.event.addListener(marker, 'click', function() {
  193.  
  194.             // Close the previously opened info window
  195.             $('.infoBox').hide();
  196.  
  197.             infowindow.open( map, marker );
  198.  
  199.         });
  200.  
  201.         // On map click
  202.         google.maps.event.addListener(map, 'click', function(event) {
  203.  
  204.             // close info window when map is clicked
  205.             if(infowindow) {
  206.                 infowindow.close();
  207.             }
  208.         });
  209.  
  210.         google.maps.event.addListener(infowindow, 'domready', function(){
  211.  
  212.             // Fade in info window
  213.             $('.info-window').hide().fadeIn('slow');
  214.  
  215.             // Allow overflow within infowindow
  216.             // NOTE: not needed with InfoBox
  217.             // $('.gm-style-iw').children().css('overflow','visible')
  218.             // $('.info-window--image').parent().css('overflow','visible');
  219.  
  220.         });
  221.  
  222.     }
  223.  
  224.   }
  225.  
  226.   /*
  227.   *  centerMap
  228.   *
  229.   *  This function will center the map, showing all markers attached to this map
  230.   */
  231.  
  232.   function centerMap( map ) {
  233.  
  234.     var bounds = new google.maps.LatLngBounds();
  235.     var visibleMarkers = map.markers.filter(function(marker){
  236.             if (marker.iconType === 'large-pin'){
  237.                 return marker.map;
  238.             }
  239.       });
  240.  
  241.       // in case no large-pins are set ensure all markers are taken into consideration
  242.       if(!visibleMarkers.length){
  243.           var visibleMarkers = map.markers.filter(function(marker){
  244.             return marker.map;
  245.           });
  246.       }
  247.  
  248.  
  249.     // loop through all markers and create bounds
  250.     $.each( visibleMarkers, function( i, marker ){
  251.         bounds.extend( marker.position );
  252.     });
  253.  
  254.     // only 1 marker?
  255.     if( visibleMarkers.length === 1 ) {
  256.         // set center of map
  257.         map.setCenter( bounds.getCenter() );
  258.         // map.setZoom( 16 );
  259.         map.setZoom( 14 );
  260.  
  261.     } else {
  262.  
  263.         // fit to bounds
  264.         map.fitBounds( bounds );
  265.  
  266.         if (!$('.js-hood-map').length){
  267.             // pull map out one zoom level - on all BUT neighborhood map (.js-hood-map)
  268.             var listener = google.maps.event.addListener(map, 'idle', function() {
  269.                 var oldZoom = map.getZoom();
  270.                 map.setZoom(oldZoom - 1);
  271.                 google.maps.event.removeListener(listener);
  272.             });
  273.         }
  274.  
  275.     }
  276.  
  277.   }
  278.  
  279.  
  280.   return {
  281.     newMap: newMap,
  282.     centerMap: centerMap,
  283.     initSequence: initSequence
  284.   };
  285.  
  286. })(jQuery);
  287.  
  288. // Added to global scope so google maps callback can hit it
  289. function initAllMosaicMaps(){
  290.     app.googlemaps.initSequence()
  291. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement