Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. <script type="text/javascript">
  2. (function() {
  3.  
  4. window.onload = function() {
  5. var mc;
  6. // Creating an object literal containing the properties we want to pass to the map
  7. var options = {
  8. zoom: 10,
  9. center: new google.maps.LatLng(52.40, -3.61),
  10. mapTypeId: google.maps.MapTypeId.ROADMAP
  11. };
  12.  
  13. // Creating the map
  14. var map = new google.maps.Map(document.getElementById('map'), options);
  15.  
  16. // Creating a LatLngBounds object
  17. var bounds = new google.maps.LatLngBounds();
  18.  
  19. // Creating an array that will contain the addresses
  20. var places = [];
  21.  
  22. // Creating a variable that will hold the InfoWindow object
  23. var infowindow;
  24. mc = new MarkerClusterer(map);
  25. <?php
  26. $pages = get_pages(array('child_of' => $post->ID, 'sort_column' => 'menu_order'));
  27. $popup_content = array();
  28. foreach($pages as $post)
  29. {
  30. setup_postdata($post);
  31. $fields = get_fields();
  32. $popup_content[] = '<p>'.$fields->company_name.'</p><img src="'.$fields->company_logo.'" /><br /><br /><a href="'.get_page_link($post->ID).'">View profile</a>';
  33. $comma = ", ";
  34. $full_address = "{$fields->address_line_1}{$comma}{$fields->address_line_2}{$comma}{$fields->address_line_3}{$comma}{$fields->post_code}";
  35. $address[] = $full_address;
  36. }
  37. wp_reset_query();
  38. echo 'var popup_content = ' . json_encode($popup_content) . ';';
  39. echo 'var address = ' . json_encode($address) . ';';
  40. ?>
  41.  
  42. var geocoder = new google.maps.Geocoder();
  43.  
  44. var markers = [];
  45.  
  46. // Adding a LatLng object for each city
  47. for (var i = 0; i < address.length; i++) {
  48. (function(i) {
  49. geocoder.geocode( {'address': address[i]}, function(results, status) {
  50. if (status == google.maps.GeocoderStatus.OK) {
  51. places[i] = results[0].geometry.location;
  52.  
  53. // Adding the markers
  54. var marker = new google.maps.Marker({position: places[i], map: map});
  55. markers.push(marker);
  56. mc.addMarker(marker);
  57.  
  58. // Creating the event listener. It now has access to the values of i and marker as they were during its creation
  59. google.maps.event.addListener(marker, 'click', function() {
  60. // Check to see if we already have an InfoWindow
  61. if (!infowindow) {
  62. infowindow = new google.maps.InfoWindow();
  63. }
  64.  
  65. // Setting the content of the InfoWindow
  66. infowindow.setContent(popup_content[i]);
  67.  
  68. // Tying the InfoWindow to the marker
  69. infowindow.open(map, marker);
  70. });
  71.  
  72. // Extending the bounds object with each LatLng
  73. bounds.extend(places[i]);
  74.  
  75. // Adjusting the map to new bounding box
  76. map.fitBounds(bounds)
  77. } else {
  78. alert("Geocode was not successful for the following reason: " + status);
  79. }
  80.  
  81. });
  82.  
  83. })(i);
  84.  
  85. }
  86. var markerCluster = new MarkerClusterer(map, markers);
  87. }
  88. })
  89. ();
  90. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement