CVDesign

Untitled

Aug 31st, 2020
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 5.41 KB | None | 0 0
  1. <?php
  2. /**
  3.  * MAP
  4.  *
  5.  * @package Expliciet
  6.  * @subpackage Expl-theme
  7.  */
  8.  
  9. $arg = array(
  10.     'post_type' => 'locations',
  11.     'post_status' => 'publish',
  12.     'orderby' => 'date',
  13.     'order' => 'DESC',
  14.     'posts_per_page' => 0,
  15. );
  16. $query = new WP_Query( $arg );
  17. if( $query->have_posts() ) {
  18.     while( $query->have_posts() ) {
  19.         $query->the_post();
  20.         $companies[] = get_field('name');
  21.         $locations[] = get_field('adress');
  22.     }
  23.     wp_reset_postdata();
  24. }
  25.  
  26. // TESTING VARS BEGIN
  27. //var_dump($companies);
  28. //foreach( $locations as $i=>$location) {
  29. //    echo 'Title: ';
  30. //    echo $location['title'];
  31. //}
  32. //foreach( $locations as $i=>$location) {
  33. //    echo 'Company: ';
  34. //    echo $companies[$i];
  35. //    echo '<br/>';
  36. //    echo 'Lat: ';
  37. //    echo $location['lat'];
  38. //    echo '<br/>';
  39. //    echo 'Lng: ';
  40. //    echo $location['lng'];
  41. //    echo '<br/>';echo '<br/>';
  42. //}
  43. // TESTING VARS ENDS
  44.  
  45. ?>
  46.  
  47. <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=<?php the_field( 'google_map_api', 'option' ); ?>"></script> <!-- Vergeet geen API-sleutel aan te maken! (js?key=) -->
  48.  
  49. <script>
  50.  
  51.     google.maps.event.addDomListener(window, 'load', init);
  52.  
  53.     function init() {
  54.         var w = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
  55.         var isDraggable = w > 1024 ? true : false;
  56.  
  57.         var mapOptions = {
  58.             scrollwheel: false,
  59.             draggable: isDraggable,
  60.             streetViewControl: false,
  61.             styles:  [
  62.                 {"featureType":"landscape","elementType":"labels","stylers":[{"visibility":"off"}]},
  63.                 {"featureType":"landscape","elementType":"all","stylers":[{"color":"#e9e9e9"}]},
  64.                 {"featureType":"transit","elementType":"labels","stylers":[{"visibility":"off"}]},
  65.                 {"featureType":"poi","elementType":"labels","stylers":[{"visibility":"off"}]},
  66.                 {"featureType":"water","elementType":"labels","stylers":[{"visibility":"off"}]},
  67.                 {"featureType":"road","elementType":"labels.icon","stylers":[{"visibility":"off"}]},{"stylers":[{"hue":"#000"},{"saturation":-100},{"lightness":10}]},
  68.                 {"featureType":"road","elementType":"labels.text.fill","stylers":[{"visibility":"on"},{"lightness":24}]},
  69.                 {"featureType":"road","elementType":"geometry","stylers":[{"lightness":10}]}
  70.             ]
  71.         };
  72.  
  73.         var mapElement = document.querySelector('.dealersmap');
  74.         var map = new google.maps.Map(mapElement, mapOptions);
  75.  
  76.         //var infowindow = new google.maps.InfoWindow();
  77.         var bounds = new google.maps.LatLngBounds();
  78.  
  79.         var icon = {
  80.             path: "M192 96c-52.935 0-96 43.065-96 96s43.065 96 96 96 96-43.065 96-96-43.065-96-96-96zm0 160c-35.29 0-64-28.71-64-64s28.71-64 64-64 64 28.71 64 64-28.71 64-64 64zm0-256C85.961 0 0 85.961 0 192c0 77.413 26.97 99.031 172.268 309.67 9.534 13.772 29.929 13.774 39.465 0C357.03 291.031 384 269.413 384 192 384 85.961 298.039 0 192 0zm0 473.931C52.705 272.488 32 256.494 32 192c0-42.738 16.643-82.917 46.863-113.137S149.262 32 192 32s82.917 16.643 113.137 46.863S352 149.262 352 192c0 64.49-20.692 80.47-160 281.931z",
  81.             fillColor: '#3371d9',
  82.             fillOpacity: 1,
  83.             strokeWeight: 0,
  84.             scale: 0.075
  85.         }
  86.         var iconHover = {
  87.             path: "M172.268 501.67C26.97 291.031 0 269.413 0 192 0 85.961 85.961 0 192 0s192 85.961 192 192c0 77.413-26.97 99.031-172.268 309.67-9.535 13.774-29.93 13.773-39.464 0zM192 272c44.183 0 80-35.817 80-80s-35.817-80-80-80-80 35.817-80 80 35.817 80 80 80z",
  88.             fillColor: '#00b4aa',
  89.             fillOpacity: 1,
  90.             strokeWeight: 0,
  91.             scale: 0.075
  92.         }
  93.  
  94.         var markers = []
  95.         var location = '';
  96.  
  97.         <?php foreach( $locations as $i=>$location) : ?>
  98.  
  99.         var infoWindow = new google.maps.InfoWindow({
  100.             content: '<?= $companies[$i];?>',
  101.         });
  102.         marker = new google.maps.Marker({
  103.             map:map,
  104.             icon: icon,
  105.             clickable: true,
  106.             title: '<?= $companies[$i]; ?>',
  107.             position: new google.maps.LatLng(<?= $location['lat']; ?>, <?= $location['lng']; ?>)
  108.         });
  109.  
  110.         google.maps.event.addListener(marker, 'click', function() {
  111.             infoWindow.open(map, marker);
  112.         });
  113.  
  114.         bounds.extend(marker.position);
  115.         markers.push(marker);
  116.  
  117.  
  118.         <?php endforeach; ?>
  119.  
  120.  
  121.  
  122.         map.fitBounds(bounds);
  123.  
  124.         // var listener = google.maps.event.addListener(map, "idle", function () {
  125.         //     map.setZoom(11);
  126.         //     google.maps.event.removeListener(listener);
  127.         // });
  128.  
  129.         if ( document.querySelector( '.locations') ) {
  130.  
  131.             [...markers].forEach( marker => {
  132.  
  133.                 marker.addListener( 'mouseover', function(e) {
  134.                     location = marker.title;
  135.                     marker.setIcon( iconHover );
  136.                     document.querySelector( `[data-location=${location}]`).dataset.active = true;
  137.                 });
  138.  
  139.                 marker.addListener( 'mouseout', function(e) {
  140.                     location = marker.title;
  141.                     marker.setIcon( icon );
  142.                     delete document.querySelector( `[data-location=${location}]`).dataset.active;
  143.                 });
  144.             })
  145.         }
  146.  
  147.     }
  148.  
  149. </script>
  150. <div class="dealersmap"></div>
Add Comment
Please, Sign In to add comment