not really known
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

map.php 3.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. }
  14. $query = new WP_Query($options);
  15. ?>
  16. <div id="talmap" style="height: 400px; width: 100%; margin: 0 0 50px 0;"></div><!-- leaflet.js map -->
  17. <script>
  18. <!--
  19. <?php
  20. $i = 0;
  21. while ( $query->have_posts() ) : $query->the_post() ;
  22. $i++;
  23. if ( $i == 1 ) {
  24. if ( $id ) {
  25. $meta = get_post_meta( get_the_ID() );
  26. $centre_lat = $meta['geo_latitude'][0];
  27. $centre_lon = $meta['geo_longitude'][0];
  28. // Get zoom level relative to distance covered
  29. $miles = $meta['distance_covered_miles'][0];
  30. switch( $miles ) {
  31. case ( $miles <= 0.1 ):
  32. $zoom = 16;
  33. break;
  34. case ( $miles <= 0.5 ):
  35. $zoom = 14;
  36. break;
  37. case ( $miles <= 1.0 ):
  38. $zoom = 13;
  39. break;
  40. case ( $miles <= 2.0 ):
  41. $zoom = 12;
  42. break;
  43. case ( $miles <= 3.0 ):
  44. $zoom = 11;
  45. break;
  46. case ( $miles <= 5.0 ):
  47. $zoom = 11;
  48. break;
  49. default:
  50. $zoom = 10;
  51. }
  52. } else {
  53. $centre_lat = 54.0;
  54. $centre_lon = 0;
  55. $zoom = 5;
  56. }
  57. ?>
  58. var map = L.map('talmap').setView([<?php echo $centre_lat ?>, <?php echo $centre_lon ?>],<?php echo $zoom ?> );
  59. L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  60. 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>',
  61. maxZoom: 18
  62. }).addTo(map);
  63. <?php
  64. }
  65. $meta = get_post_meta( get_the_ID() );
  66. $link = sprintf( "<a href=\'%s\'>%s</a>", get_permalink(), get_the_title() );
  67. echo sprintf(
  68. "var marker = L.marker([%f, %f]).addTo(map);\nmarker.bindPopup('%s');",
  69. $meta['geo_latitude'][0],
  70. $meta['geo_longitude'][0],
  71. $link
  72. );
  73. endwhile;
  74. if ( $id ):
  75. 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 );
  76. endif;
  77. ?>
  78. -->
  79. </script>
  80. <?php
  81. if ( AS_DEBUG ) echo "<h1>Zoom: $zoom Miles: $miles</h1>";
  82. }
  83. add_shortcode( 'talmap', 'talhyperlocal_map_shortcode' );
  84. function talhyperlocal_enqueue()
  85. {
  86. wp_register_script( 'leaflet-js', 'http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js' );
  87. wp_register_style( 'leaflet-css', 'http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css' );
  88. wp_enqueue_style( 'leaflet-css' );
  89. wp_enqueue_script( 'leaflet-js' );
  90. }
  91. add_action( 'wp_enqueue_scripts', 'talhyperlocal_enqueue' );