Advertisement
Guest User

Untitled

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