not really known
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

86 строки
2.2 KiB

  1. <?php
  2. function talhyperlocal_map_shortcode( $id ) {
  3. $options = array(
  4. 'post_type' => 'site',
  5. 'posts_per_page' => -1, // -1 for all posts
  6. 'post_status' => 'publish',
  7. 'orderby' => 'title',
  8. 'order' => 'ASC'
  9. );
  10. if ( $id ) {
  11. $options['p'] = $id;
  12. }
  13. $query = new WP_Query($options);
  14. ?>
  15. <div id="talmap" style="height: 400px; width: 100%; margin: 0 0 50px 0;"></div><!-- leaflet.js map -->
  16. <script>
  17. <?php
  18. $i = 0;
  19. while ( $query->have_posts() ) : $query->the_post() ;
  20. $i++;
  21. if ( $i == 1 ) {
  22. if ( $id ) {
  23. $meta = get_post_meta( get_the_ID() );
  24. $centre_lat = $meta['geo_latitude'][0];
  25. $centre_lon = $meta['geo_longitude'][0];
  26. $zoom = 11;
  27. } else {
  28. $centre_lat = 54.0;
  29. $centre_lon = 0;
  30. $zoom = 5;
  31. }
  32. ?>
  33. var map = L.map('talmap').setView([<?php echo $centre_lat ?>, <?php echo $centre_lon ?>],<?php echo $zoom ?> );
  34. L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  35. 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>',
  36. maxZoom: 18
  37. }).addTo(map);
  38. <?php
  39. }
  40. $meta = get_post_meta( get_the_ID() );
  41. $link = sprintf( "<a href=\'%s\'>%s</a>", get_permalink(), get_the_title() );
  42. echo sprintf(
  43. "var marker = L.marker([%f, %f]).addTo(map);\nmarker.bindPopup('%s');",
  44. $meta['geo_latitude'][0],
  45. $meta['geo_longitude'][0],
  46. $link
  47. );
  48. endwhile;
  49. if ( $id ):
  50. 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 );
  51. endif;
  52. ?>
  53. </script>
  54. <table>
  55. <?php
  56. }
  57. add_shortcode( 'talmap', 'talhyperlocal_map_shortcode' );
  58. function talhyperlocal_enqueue()
  59. {
  60. wp_register_script( 'leaflet-js', 'http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js' );
  61. wp_register_style( 'leaflet-css', 'http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css' );
  62. wp_enqueue_style( 'leaflet-css' );
  63. wp_enqueue_script( 'leaflet-js' );
  64. }
  65. add_action( 'wp_enqueue_scripts', 'talhyperlocal_enqueue' );