Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script type="text/javascript">
  2. (function($) {
  3. /*
  4. *  new_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 offsetCenter(map, latlng, offsetx, offsety) {
  17.  
  18.     var scale = Math.pow(2, map.getZoom());
  19.  
  20.     var worldCoordinateCenter = map.getProjection().fromLatLngToPoint(latlng);
  21.     var pixelOffset = new google.maps.Point((offsetx/scale) || 0,(offsety/scale) ||0)
  22.  
  23.     var worldCoordinateNewCenter = new google.maps.Point(
  24.         worldCoordinateCenter.x - pixelOffset.x,
  25.         worldCoordinateCenter.y + pixelOffset.y
  26.     );
  27.  
  28.     var newCenter = map.getProjection().fromPointToLatLng(worldCoordinateNewCenter);
  29.  
  30.     map.setCenter(newCenter);
  31.  
  32. }
  33.  
  34. function new_map( $el ) {
  35.  
  36.     // var
  37.     var $markers = $el.find('.marker');
  38.  
  39.  
  40.     // vars
  41.     var args = {
  42.         zoom        : 16, //default zoom
  43.         center      : new google.maps.LatLng(0, 0),
  44.         mapTypeControl: false, //disable map view
  45.         streetViewControl: false, //disable street view
  46.         mapTypeId   : google.maps.MapTypeId.SATELLITE
  47.     };
  48.  
  49.  
  50.     // create map
  51.     var map = new google.maps.Map( $el[0], args);
  52.  
  53.  
  54.     // add a markers reference
  55.     map.markers = [];
  56.  
  57.  
  58.     // add markers
  59.     $markers.each(function(index){
  60.  
  61.         add_marker( $(this), map, index);
  62.  
  63.     });
  64.  
  65.  
  66.   // center map based on all markers
  67.   //center_map( map );
  68.  
  69.   // center map to first marker position
  70.   center_map_first_marker(map)
  71.  
  72.     // return
  73.     return map;
  74.  
  75. }
  76.  
  77. /*
  78. *  add_marker
  79. *
  80. *  This function will add a marker to the selected Google Map
  81. *
  82. *  @type    function
  83. *  @date    8/11/2013
  84. *  @since   4.3.0
  85. *
  86. *  @param   $marker (jQuery element)
  87. *  @param   map (Google Map object)
  88. *  @return  n/a
  89. */
  90.  
  91. function add_marker( $marker, map, index ) {
  92.     // var
  93.     var latlng = new google.maps.LatLng( $marker.attr('data-lat'), $marker.attr('data-lng') );
  94.   var eventType = is_touch_device() ? 'click' : 'mouseover';
  95.  
  96.     // if marker contains HTML, add it to an infoWindow
  97.     if( $marker.html() ) {
  98.     var infowindow = new google.maps.InfoWindow({ content: $marker.html()});
  99.     var marker = new google.maps.Marker({
  100.       position: latlng,
  101.       map: map<?php if( $default_marker_icon ): ?>,
  102.       icon:  '<?php echo $default_marker_icon; ?>'<?php endif; ?>,
  103.       infowindow: infowindow
  104.     });
  105.         // show info window when marker is hovered over
  106.         google.maps.event.addListener(marker, eventType, function() {
  107.       hideAllInfoWindows(map);
  108.             this.infowindow.open( map, marker );
  109.         });
  110.  
  111.     if (index === 0 ) {
  112.             marker.infowindow.open( map, marker );
  113.     }
  114.  
  115.     } else {
  116.     var marker = new google.maps.Marker({
  117.       position  : latlng,
  118.       map           : map<?php if( $default_marker_icon ): ?>,
  119.       icon:  '<?php echo $default_marker_icon; ?>'<?php endif; ?>
  120.     });
  121.   }
  122.  
  123.   // add to array
  124.   map.markers.push( marker );
  125.  
  126. }
  127.  
  128. function hideAllInfoWindows(map) {
  129.   map.markers.forEach(function(marker) {
  130.     if (!!marker.infowindow) {
  131.       marker.infowindow.close(map, marker);
  132.     }
  133.   });
  134. }
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141. /*
  142. *  center_map
  143. *
  144. *  This function will center the map, showing all markers attached to this map
  145. *
  146. *  @type    function
  147. *  @date    8/11/2013
  148. *  @since   4.3.0
  149. *
  150. *  @param   map (Google Map object)
  151. *  @return  n/a
  152. */
  153.  
  154. function center_map( map ) {
  155.  
  156.     // vars
  157.     var bounds = new google.maps.LatLngBounds();
  158.  
  159.     // loop through all markers and create bounds
  160.     $.each( map.markers, function( i, marker ){
  161.  
  162.         var latlng = new google.maps.LatLng( marker.position.lat(), marker.position.lng() );
  163.  
  164.         bounds.extend( latlng );
  165.  
  166.     });
  167.  
  168.     // only 1 marker?
  169.     if( map.markers.length === 1 )
  170.     {
  171.         // set center of map
  172.         map.setCenter( bounds.getCenter() );
  173.         map.setZoom( 16 ); //zoom for single marker case, if you use perv function
  174.     }
  175.     else
  176.     {
  177.         // fit to bounds
  178.         map.fitBounds( bounds );
  179.     }
  180. }
  181.  
  182. function center_map_first_marker( map ) {
  183.   // vars
  184.   if( map.markers.length) {
  185.     var marker = map.markers[0];
  186.     var latlng = new google.maps.LatLng( marker.position.lat(), marker.position.lng() );
  187.     // set center of map
  188.     //   map.setCenter( latlng );
  189.       offsetCenter(map, latlng, 0 200);
  190.       map.setZoom( 14 );
  191.  
  192.   }
  193. }
  194.  
  195. function is_touch_device() {
  196.  return (('ontouchstart' in window)
  197.       || (navigator.MaxTouchPoints > 0)
  198.       || (navigator.msMaxTouchPoints > 0));
  199. }
  200.  
  201. /*
  202. *  document ready
  203. *
  204. *  This function will render each map when the document is ready (page has loaded)
  205. *
  206. *  @type    function
  207. *  @date    8/11/2013
  208. *  @since   5.0.0
  209. *
  210. *  @param   n/a
  211. *  @return  n/a
  212. */
  213. // global var
  214. var map = null;
  215.  
  216. $(document).ready(function(){
  217.  
  218.     $('.acf-map').each(function(){
  219.  
  220.         // create map
  221.         map = new_map( $(this) );
  222.  
  223.     });
  224.  
  225. });
  226.  
  227. })(jQuery);
  228. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement