Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function($) {
  2.  
  3. /*
  4. *  render_map
  5. *
  6. *  This function will render a Google Map onto the selected jQuery element
  7. *
  8. *  @type    function
  9. *  @date    8/11/2013
  10. *  @since   4.3.0
  11. *
  12. *  @param   $el (jQuery element)
  13. *  @return  n/a
  14. */
  15.  
  16. function render_map( $el ) {
  17.  
  18.     // var
  19.     var $markers = $el.find('.marker');
  20.  
  21.     // vars
  22.     var args = {
  23.         zoom        : 16,
  24.         center      : new google.maps.LatLng(0, 0),
  25.         mapTypeId   : google.maps.MapTypeId.ROADMAP
  26.     };
  27.  
  28.     // create map              
  29.     var map = new google.maps.Map( $el[0], args);
  30.  
  31.     // add a markers reference
  32.     map.markers = [];
  33.  
  34.     // add markers
  35.     $markers.each(function(){
  36.  
  37.         add_marker( $(this), map );
  38.  
  39.     });
  40.  
  41.     // center map
  42.     center_map( map );
  43.  
  44. }
  45.  
  46. /*
  47. *  add_marker
  48. *
  49. *  This function will add a marker to the selected Google Map
  50. *
  51. *  @type    function
  52. *  @date    8/11/2013
  53. *  @since   4.3.0
  54. *
  55. *  @param   $marker (jQuery element)
  56. *  @param   map (Google Map object)
  57. *  @return  n/a
  58. */
  59.  
  60. function add_marker( $marker, map ) {
  61.  
  62.     // var
  63.     var latlng = new google.maps.LatLng( $marker.attr('data-lat'), $marker.attr('data-lng') );
  64.  
  65.     // create marker
  66.     var marker = new google.maps.Marker({
  67.         position    : latlng,
  68.         map         : map
  69.     });
  70.  
  71.     // add to array
  72.     map.markers.push( marker );
  73.  
  74.     // if marker contains HTML, add it to an infoWindow
  75.     if( $marker.html() )
  76.     {
  77.         // create info window
  78.         var infowindow = new google.maps.InfoWindow({
  79.             content     : $marker.html()
  80.         });
  81.  
  82.         // show info window when marker is clicked
  83.         google.maps.event.addListener(marker, 'click', function() {
  84.  
  85.             infowindow.open( map, marker );
  86.  
  87.         });
  88.     }
  89.  
  90. }
  91.  
  92. /*
  93. *  center_map
  94. *
  95. *  This function will center the map, showing all markers attached to this map
  96. *
  97. *  @type    function
  98. *  @date    8/11/2013
  99. *  @since   4.3.0
  100. *
  101. *  @param   map (Google Map object)
  102. *  @return  n/a
  103. */
  104.  
  105. function center_map( map ) {
  106.  
  107.     // vars
  108.     var bounds = new google.maps.LatLngBounds();
  109.  
  110.     // loop through all markers and create bounds
  111.     $.each( map.markers, function( i, marker ){
  112.  
  113.         var latlng = new google.maps.LatLng( marker.position.lat(), marker.position.lng() );
  114.  
  115.         bounds.extend( latlng );
  116.  
  117.     });
  118.  
  119.     // only 1 marker?
  120.     if( map.markers.length == 1 )
  121.     {
  122.         // set center of map
  123.         map.setCenter( bounds.getCenter() );
  124.         map.setZoom( 16 );
  125.     }
  126.     else
  127.     {
  128.         // fit to bounds
  129.         map.fitBounds( bounds );
  130.     }
  131.  
  132. }
  133.  
  134. /*
  135. *  document ready
  136. *
  137. *  This function will render each map when the document is ready (page has loaded)
  138. *
  139. *  @type    function
  140. *  @date    8/11/2013
  141. *  @since   5.0.0
  142. *
  143. *  @param   n/a
  144. *  @return  n/a
  145. */
  146.  
  147. $(document).ready(function(){
  148.  
  149.     $('.acf-map').each(function(){
  150.  
  151.         render_map( $(this) );
  152.  
  153.     });
  154.  
  155. });
  156.  
  157. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement