daymobrew

Google Maps shortcode

Feb 3rd, 2014
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.03 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Google Maps shortcode
  4. Plugin URI: http://www.damiencarbery.com
  5. Description: Shortcode for Google map.
  6. Author: Damien Carbery
  7. Version: 0.1
  8.  
  9. $Id: map_shortcode.php 2309 2014-01-27 08:31:57Z damien $
  10.  
  11. Usage:
  12. [dc_map zoom="15" location="53.302343, -6.177633" height="300" marker="yes" marker_title="This does not appear"]
  13.  
  14.  
  15. */
  16.  
  17. add_action('wp_head', 'add_google_maps');
  18. function add_google_maps() {
  19. ?>
  20. <script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
  21. <?php
  22. }
  23.     /** Map
  24.       *
  25.       * Creates and inserts a Google Maps map into the content.
  26.       *
  27.       * @param array $atts The attributes passed to the shortcode
  28.       * @param string $content The content the shortcode is wrapped around
  29.       *
  30.       *
  31.       * @uses prepare_margin()
  32.       *
  33.       * @return string $output The resulting HTML code
  34.       *
  35.       */
  36. function dc_shortcode_map( $atts, $content ) {
  37.  
  38.         // Extract the attributes into variables
  39.         $atts = shortcode_atts( array(
  40.             'sample'    => false,
  41.             //'margin' => $this->shortcodes['map']['margin'],
  42.             'location' => '',
  43.             'zoom' => 12,
  44.             'maptype' => 'ROADMAP',
  45.             'marker' => 'no',
  46.             'marker_title' => '',
  47.             'height' => 100,
  48.             'style' => '',
  49.          ), $atts );
  50.  
  51.         extract( $atts );
  52.  
  53.         // Prepare the style and class display and the output
  54.         $style_array = array(); $class_array = array(); $output = '';
  55.  
  56.         // Create Output
  57.         $style_array['height'] = 'height: ' . $height . 'px';
  58.         //$style_array['margin'] = 'margin: ' . $this->prepare_margin( $margin );
  59.         $style_string = implode( '; ', $style_array ) ;
  60.         $style_string .= '; ' . $style;
  61.  
  62.         $location_request = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode( $location ) . '&sensor=false';
  63.         $location_info = json_decode( file_get_contents( $location_request ) );
  64.         $geocode = $location_info->results[0]->geometry->location;
  65.  
  66.         $id = rand(12345,99999);
  67.         $output .='
  68.         <script>
  69.                 var coordinates_' . $id . ' = new google.maps.LatLng(' . $geocode->lat . ', ' . $geocode->lng . ');
  70.                 var mapOptions_' . $id . ' = {
  71.                     zoom: ' . $zoom . ',
  72.                     center: coordinates_' . $id . ',
  73.                     mapTypeId: google.maps.MapTypeId.' . strtoupper( $maptype ) . '
  74.                 }
  75.         ';
  76.         if( $marker == "yes" ) {
  77.             $output .= '
  78.                 var marker_' . $id . ' = new google.maps.Marker({
  79.                     position: coordinates_' . $id . ',
  80.                     title:"'.$marker_title.'"
  81.                 });
  82.             ';
  83.         }
  84.  
  85.         $output .= '
  86.  
  87.                 jQuery(window).load( function() {
  88.                     map_' . $id . ' = new google.maps.Map(document.getElementById("map-'.$id.'"), mapOptions_' . $id . ')
  89.                     if( typeof marker_' . $id . ' != "undefined" && marker_' . $id . ' != "" ) {
  90.                         marker_' . $id . '.setMap(map_' . $id . ');
  91.                     }
  92.                 })
  93.         ';
  94.  
  95.         $output .= '
  96.         </script>
  97.         ';
  98.  
  99.         $output .= '<div class="map">';
  100.         $output .= '<div class="map_canvas googlemap" style="' . $style_string . '" id="map-'.$id.'"></div>';
  101.         $output .= '</div>';
  102.  
  103.         return $output;
  104.     }
  105. add_shortcode( 'dc_map', 'dc_shortcode_map' );
  106. ?>
Add Comment
Please, Sign In to add comment