not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.0 KiB

  1. <?php
  2. define( 'AS_DEBUG', false );
  3. function talhyperlocal_map_shortcode( $id ) {
  4. $options = array(
  5. 'post_type' => 'site',
  6. 'posts_per_page' => -1, // -1 for all posts
  7. 'post_status' => 'publish',
  8. 'orderby' => 'title',
  9. 'order' => 'ASC'
  10. );
  11. if ( $id ) {
  12. $options['p'] = $id;
  13. $options['post_status'] = array( 'publish', 'archive' );
  14. }
  15. $query = new WP_Query($options);
  16. ?>
  17. <div id="talmap" style="height: 400px; width: 100%; margin: 0 0 50px 0;"></div><!-- leaflet.js map -->
  18. <script>
  19. <!--
  20. <?php
  21. $i = 0;
  22. while ( $query->have_posts() ) : $query->the_post() ;
  23. $i++;
  24. if ( $i == 1 ) {
  25. if ( $id ) {
  26. $meta = get_post_meta( get_the_ID() );
  27. $centre_lat = $meta['geo_latitude'][0];
  28. $centre_lon = $meta['geo_longitude'][0];
  29. // Get zoom level relative to distance covered
  30. $miles = $meta['distance_covered_miles'][0];
  31. switch( $miles ) {
  32. case ( $miles <= 0.1 ):
  33. $zoom = 16;
  34. break;
  35. case ( $miles <= 0.5 ):
  36. $zoom = 14;
  37. break;
  38. case ( $miles <= 1.0 ):
  39. $zoom = 13;
  40. break;
  41. case ( $miles <= 2.0 ):
  42. $zoom = 12;
  43. break;
  44. case ( $miles <= 3.0 ):
  45. $zoom = 11;
  46. break;
  47. case ( $miles <= 5.0 ):
  48. $zoom = 11;
  49. break;
  50. default:
  51. $zoom = 10;
  52. }
  53. } else {
  54. $centre_lat = 54.0;
  55. $centre_lon = 0;
  56. $zoom = 5;
  57. }
  58. ?>
  59. var map = L.map('talmap').setView([<?php echo $centre_lat ?>, <?php echo $centre_lon ?>],<?php echo $zoom ?> );
  60. L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  61. attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>',
  62. maxZoom: 18
  63. }).addTo(map);
  64. <?php
  65. }
  66. $meta = get_post_meta( get_the_ID() );
  67. $link = sprintf( "<a href=\'%s\'>%s</a>", get_permalink(), get_the_title() );
  68. echo sprintf(
  69. "var marker = L.marker([%f, %f]).addTo(map);\nmarker.bindPopup('%s');",
  70. $meta['geo_latitude'][0],
  71. $meta['geo_longitude'][0],
  72. $link
  73. );
  74. endwhile;
  75. if ( $id ):
  76. echo sprintf("var circle = L.circle([%f, %f], %d, { color: 'red', fillColor: '#f03', fillOpacity: 0.2 }).addTo(map);", $meta['geo_latitude'][0], $meta['geo_longitude'][0], $meta['distance_covered_miles'][0] * 1609.344 );
  77. endif;
  78. ?>
  79. -->
  80. </script>
  81. <?php
  82. if ( AS_DEBUG ) echo "<h1>Zoom: $zoom Miles: $miles</h1>";
  83. }
  84. add_shortcode( 'talmap', 'talhyperlocal_map_shortcode' );
  85. function talhyperlocal_enqueue()
  86. {
  87. wp_register_script( 'leaflet-js', 'http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js' );
  88. wp_register_style( 'leaflet-css', 'http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css' );
  89. wp_enqueue_style( 'leaflet-css' );
  90. wp_enqueue_script( 'leaflet-js' );
  91. }
  92. add_action( 'wp_enqueue_scripts', 'talhyperlocal_enqueue' );