Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Google Maps shortcode
- Plugin URI: http://www.damiencarbery.com
- Description: Shortcode for Google map.
- Author: Damien Carbery
- Version: 0.1
- $Id: map_shortcode.php 2309 2014-01-27 08:31:57Z damien $
- Usage:
- [dc_map zoom="15" location="53.302343, -6.177633" height="300" marker="yes" marker_title="This does not appear"]
- */
- add_action('wp_head', 'add_google_maps');
- function add_google_maps() {
- ?>
- <script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
- <?php
- }
- /** Map
- *
- * Creates and inserts a Google Maps map into the content.
- *
- * @param array $atts The attributes passed to the shortcode
- * @param string $content The content the shortcode is wrapped around
- *
- *
- * @uses prepare_margin()
- *
- * @return string $output The resulting HTML code
- *
- */
- function dc_shortcode_map( $atts, $content ) {
- // Extract the attributes into variables
- $atts = shortcode_atts( array(
- 'sample' => false,
- //'margin' => $this->shortcodes['map']['margin'],
- 'location' => '',
- 'zoom' => 12,
- 'maptype' => 'ROADMAP',
- 'marker' => 'no',
- 'marker_title' => '',
- 'height' => 100,
- 'style' => '',
- ), $atts );
- extract( $atts );
- // Prepare the style and class display and the output
- $style_array = array(); $class_array = array(); $output = '';
- // Create Output
- $style_array['height'] = 'height: ' . $height . 'px';
- //$style_array['margin'] = 'margin: ' . $this->prepare_margin( $margin );
- $style_string = implode( '; ', $style_array ) ;
- $style_string .= '; ' . $style;
- $location_request = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode( $location ) . '&sensor=false';
- $location_info = json_decode( file_get_contents( $location_request ) );
- $geocode = $location_info->results[0]->geometry->location;
- $id = rand(12345,99999);
- $output .='
- <script>
- var coordinates_' . $id . ' = new google.maps.LatLng(' . $geocode->lat . ', ' . $geocode->lng . ');
- var mapOptions_' . $id . ' = {
- zoom: ' . $zoom . ',
- center: coordinates_' . $id . ',
- mapTypeId: google.maps.MapTypeId.' . strtoupper( $maptype ) . '
- }
- ';
- if( $marker == "yes" ) {
- $output .= '
- var marker_' . $id . ' = new google.maps.Marker({
- position: coordinates_' . $id . ',
- title:"'.$marker_title.'"
- });
- ';
- }
- $output .= '
- jQuery(window).load( function() {
- map_' . $id . ' = new google.maps.Map(document.getElementById("map-'.$id.'"), mapOptions_' . $id . ')
- if( typeof marker_' . $id . ' != "undefined" && marker_' . $id . ' != "" ) {
- marker_' . $id . '.setMap(map_' . $id . ');
- }
- })
- ';
- $output .= '
- </script>
- ';
- $output .= '<div class="map">';
- $output .= '<div class="map_canvas googlemap" style="' . $style_string . '" id="map-'.$id.'"></div>';
- $output .= '</div>';
- return $output;
- }
- add_shortcode( 'dc_map', 'dc_shortcode_map' );
- ?>
Add Comment
Please, Sign In to add comment