Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function($) {
  2.  
  3.     // ACF Google Map
  4.     /*
  5.     *  new_map
  6.     *
  7.     *  This function will render a Google Map onto the selected jQuery element
  8.     */
  9.     function new_map( $el ) {
  10.         // var
  11.         var $markers = $el.find('.marker');
  12.        
  13.         // vars
  14.         var args = {
  15.             zoom        : 16,
  16.             center      : new google.maps.LatLng(0, 0),
  17.             scrollwheel: false,
  18.             disableDefaultUI: false,
  19.             scaleControl: false,
  20.             panControl: false,
  21.             streetViewControl: false,
  22.             styles: [{
  23.                 "featureType": "water",
  24.                 "elementType": "geometry",
  25.                 "stylers": [{
  26.                     "color": "#e9e9e9"
  27.                 }, {
  28.                     "lightness": 17
  29.                 }]
  30.             }, {
  31.                 "featureType": "landscape",
  32.                 "elementType": "geometry",
  33.                 "stylers": [{
  34.                     "color": "#f5f5f5"
  35.                 }, {
  36.                     "lightness": 20
  37.                 }]
  38.             }, {
  39.                 "featureType": "road.highway",
  40.                 "elementType": "geometry.fill",
  41.                 "stylers": [{
  42.                     "color": "#ffffff"
  43.                 }, {
  44.                     "lightness": 17
  45.                 }]
  46.             }, {
  47.                 "featureType": "road.highway",
  48.                 "elementType": "geometry.stroke",
  49.                 "stylers": [{
  50.                     "color": "#ffffff"
  51.                 }, {
  52.                     "lightness": 29
  53.                 }, {
  54.                     "weight": 0.2
  55.                 }]
  56.             }, {
  57.                 "featureType": "road.arterial",
  58.                 "elementType": "geometry",
  59.                 "stylers": [{
  60.                     "color": "#ffffff"
  61.                 }, {
  62.                     "lightness": 18
  63.                 }]
  64.             }, {
  65.                 "featureType": "road.local",
  66.                 "elementType": "geometry",
  67.                 "stylers": [{
  68.                     "color": "#ffffff"
  69.                 }, {
  70.                     "lightness": 16
  71.                 }]
  72.             }, {
  73.                 "featureType": "poi",
  74.                 "elementType": "geometry",
  75.                 "stylers": [{
  76.                     "color": "#f5f5f5"
  77.                 }, {
  78.                     "lightness": 21
  79.                 }]
  80.             }, {
  81.                 "featureType": "poi.park",
  82.                 "elementType": "geometry",
  83.                 "stylers": [{
  84.                     "color": "#dedede"
  85.                 }, {
  86.                     "lightness": 21
  87.                 }]
  88.             }, {
  89.                 "elementType": "labels.text.stroke",
  90.                 "stylers": [{
  91.                     "visibility": "on"
  92.                 }, {
  93.                     "color": "#ffffff"
  94.                 }, {
  95.                     "lightness": 16
  96.                 }]
  97.             }, {
  98.                 "elementType": "labels.text.fill",
  99.                 "stylers": [{
  100.                     "saturation": 36
  101.                 }, {
  102.                     "color": "#333333"
  103.                 }, {
  104.                     "lightness": 40
  105.                 }]
  106.             }, {
  107.                 "elementType": "labels.icon",
  108.                 "stylers": [{
  109.                     "visibility": "off"
  110.                 }]
  111.             }, {
  112.                 "featureType": "transit",
  113.                 "elementType": "geometry",
  114.                 "stylers": [{
  115.                     "color": "#f2f2f2"
  116.                 }, {
  117.                     "lightness": 19
  118.                 }]
  119.             }, {
  120.                 "featureType": "administrative",
  121.                 "elementType": "geometry.fill",
  122.                 "stylers": [{
  123.                     "color": "#fefefe"
  124.                 }, {
  125.                     "lightness": 20
  126.                 }]
  127.             }, {
  128.                 "featureType": "administrative",
  129.                 "elementType": "geometry.stroke",
  130.                 "stylers": [{
  131.                     "color": "#fefefe"
  132.                 }, {
  133.                     "lightness": 17
  134.                 }, {
  135.                     "weight": 1.2
  136.                 }]
  137.             }],
  138.             mapTypeId   : google.maps.MapTypeId.ROADMAP
  139.         };
  140.        
  141.         // create map              
  142.         var map = new google.maps.Map( $el[0], args);
  143.        
  144.         // add a markers reference
  145.         map.markers = [];
  146.        
  147.         // add markers
  148.         $markers.each(function(){
  149.             add_marker( $(this), map );
  150.         });
  151.        
  152.         // center map
  153.         center_map( map );
  154.        
  155.         // return
  156.         return map;
  157.     }
  158.  
  159.     /*
  160.     *  add_marker
  161.     *
  162.     *  This function will add a marker to the selected Google Map
  163.     */
  164.  
  165.     function add_marker( $marker, map ) {
  166.         // var
  167.         var latlng = new google.maps.LatLng( $marker.attr('data-lat'), $marker.attr('data-lng') );
  168.  
  169.         // create marker
  170.         // var image = "http://kaylagotchased.com/wp-content/uploads/2017/06/pin.svg";
  171.         var marker = new google.maps.Marker({
  172.             position    : latlng,
  173.             map         : map,
  174.             // icon        : image
  175.         });
  176.  
  177.         // add to array
  178.         map.markers.push( marker );
  179.  
  180.         // if marker contains HTML, add it to an infoWindow
  181.         if( $marker.html() )
  182.         {
  183.             // create info window
  184.             var infowindow = new google.maps.InfoWindow({
  185.                 content     : $marker.html()
  186.             });
  187.  
  188.             // show info window when marker is clicked
  189.             google.maps.event.addListener(marker, 'click', function() {
  190.  
  191.                 infowindow.open( map, marker );
  192.  
  193.             });
  194.         }
  195.     }
  196.  
  197.     /*
  198.     *  center_map
  199.     *
  200.     *  This function will center the map, showing all markers attached to this map
  201.     */
  202.  
  203.     function center_map( map ) {
  204.         // vars
  205.         var bounds = new google.maps.LatLngBounds();
  206.  
  207.         // loop through all markers and create bounds
  208.         $.each( map.markers, function( i, marker ){
  209.  
  210.             var latlng = new google.maps.LatLng( marker.position.lat(), marker.position.lng() );
  211.  
  212.             bounds.extend( latlng );
  213.  
  214.         });
  215.  
  216.         // only 1 marker?
  217.         if( map.markers.length == 1 )
  218.         {
  219.             // set center of map
  220.             map.setCenter( bounds.getCenter() );
  221.             map.setZoom( 16 );
  222.         }
  223.         else
  224.         {
  225.             // fit to bounds
  226.             map.fitBounds( bounds );
  227.         }
  228.     }
  229.  
  230.     /*
  231.     *  document ready
  232.     *
  233.     *  This function will render each map when the document is ready (page has loaded)
  234.     */
  235.     // global var
  236.     var map = null;
  237.  
  238.     // $(document).ready(function(){
  239.  
  240.     //     $('.hotel-map').each(function(){
  241.     //         // create map
  242.     //         map = new_map( $(this) );
  243.     //         // popup is shown and map is not visible
  244.     //         google.maps.event.trigger(map, 'resize');
  245.     //     });
  246.  
  247.     // });
  248.  
  249.     $('a[href="#directions"]').on('shown.bs.tab',function(){
  250.         $('.hotel-map').each(function(){
  251.             // create map
  252.             map = new_map( $(this) );
  253.             google.maps.event.trigger(map, 'resize');
  254.         });
  255.     });
  256.  
  257. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement