not really known
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.3 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. <!--
  18. <?php
  19. $i = 0;
  20. while ( $query->have_posts() ) : $query->the_post() ;
  21. $i++;
  22. if ( $i == 1 ) {
  23. if ( $id ) {
  24. $meta = get_post_meta( get_the_ID() );
  25. $centre_lat = $meta['geo_latitude'][0];
  26. $centre_lon = $meta['geo_longitude'][0];
  27. $zoom = 11;
  28. } else {
  29. $centre_lat = 54.0;
  30. $centre_lon = 0;
  31. $zoom = 5;
  32. }
  33. ?>
  34. var map = L.map('talmap').setView([<?php echo $centre_lat ?>, <?php echo $centre_lon ?>],<?php echo $zoom ?> );
  35. L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  36. 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>',
  37. maxZoom: 18
  38. }).addTo(map);
  39. <?php
  40. }
  41. $meta = get_post_meta( get_the_ID() );
  42. $link = sprintf( "<a href=\'%s\'>%s</a>", get_permalink(), get_the_title() );
  43. echo sprintf(
  44. "var marker = L.marker([%f, %f]).addTo(map);\nmarker.bindPopup('%s');",
  45. $meta['geo_latitude'][0],
  46. $meta['geo_longitude'][0],
  47. $link
  48. );
  49. endwhile;
  50. if ( $id ):
  51. 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 );
  52. endif;
  53. ?>
  54. -->
  55. </script>
  56. <?php
  57. }
  58. add_shortcode( 'talmap', 'talhyperlocal_map_shortcode' );
  59. function talhyperlocal_enqueue()
  60. {
  61. wp_register_script( 'leaflet-js', 'http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js' );
  62. wp_register_style( 'leaflet-css', 'http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css' );
  63. wp_enqueue_style( 'leaflet-css' );
  64. wp_enqueue_script( 'leaflet-js' );
  65. }
  66. add_action( 'wp_enqueue_scripts', 'talhyperlocal_enqueue' );