Beispiel #1
0
 /**
  * Add widget map
  *
  * @param array $args Args.
  * @return string Output.
  */
 function add_widget_map($args = null)
 {
     global $wpgeo, $post;
     $wp_geo_options = get_option('wp_geo_options');
     $current_post = $post->ID;
     $html_js = '';
     $markers_js = '';
     $polyline_js = '';
     $markers_js_3 = '';
     $polyline_js_3 = '';
     $args = wp_parse_args($args, array('width' => '100%', 'height' => 150, 'maptype' => empty($wp_geo_options['google_map_type']) ? 'G_NORMAL_MAP' : $wp_geo_options['google_map_type'], 'show_polylines' => false, 'zoom' => $wp_geo_options['default_map_zoom'], 'id' => 'widget_map', 'posts' => null));
     if (!$args['posts']) {
         return $html_js;
     }
     // Create Map
     $map = new WPGeo_Map($args['id']);
     $map->set_size($args['width'], $args['height']);
     $map->set_map_centre(new WPGeo_Coord(0, 0));
     $map->set_map_zoom($args['zoom']);
     $map->set_map_type($args['maptype']);
     // If Google API Key...
     if ($wpgeo->checkGoogleAPIKey()) {
         // Add points (from posts) to map
         $count = 0;
         foreach ($args['posts'] as $geo_post) {
             $coord = get_wpgeo_post_coord($geo_post->ID);
             if ($coord->is_valid_coord()) {
                 $count++;
                 if (count($count) == 1) {
                     $map->set_map_centre($coord);
                 }
                 $map->add_point($coord, array('icon' => apply_filters('wpgeo_marker_icon', 'small', $geo_post, 'widget'), 'title' => get_wpgeo_title($geo_post->ID), 'link' => apply_filters('wpgeo_marker_link', get_permalink($geo_post), $geo_post), 'post' => $geo_post));
             }
         }
         // Only show map widget if there are coords to show
         if (count($map->points) > 0) {
             // Add polylines (to connect points) to map
             if ($args['show_polylines']) {
                 $polyline = new WPGeo_Polyline(array('color' => $wp_geo_options['polyline_colour']));
                 foreach ($map->points as $point) {
                     $polyline->add_coord($point->get_coord());
                 }
                 $map->add_polyline($polyline);
             }
             $html_js .= $map->get_map_html(array('classes' => array('wp_geo_map')));
         }
         $wpgeo->maps->add_map($map);
         return $html_js;
     }
 }
Beispiel #2
0
    /**
     * @method       Add Map
     * @description  Add the map to the widget.
     * @param        $width = Map width
     * @param        $height = Map height
     * @param        $maptype = Map Type
     * @param        $showpolylines = Show Polylines
     * @param        $zoom = Zoom
     * @return       (string) HTML JavaScript.
     * @note         TO DO: integrate the code better into the existing one.
     */
    function add_map($width = '100%', $height = 150, $maptype = '', $showpolylines = false, $zoom = null, $id = 'wp_geo_map_widget')
    {
        global $posts, $wpgeo;
        $html_js = '';
        // If Google API Key...
        if ($wpgeo->checkGoogleAPIKey()) {
            // Set default width and height
            if (empty($width)) {
                $width = '100%';
            }
            if (empty($height)) {
                $height = '150';
            }
            // Get the basic settings of wp geo
            $wp_geo_options = get_option('wp_geo_options');
            // Find the coordinates for the posts
            $coords = array();
            for ($i = 0; $i < count($posts); $i++) {
                $post = $posts[$i];
                $latitude = get_post_meta($post->ID, WPGEO_LATITUDE_META, true);
                $longitude = get_post_meta($post->ID, WPGEO_LONGITUDE_META, true);
                $post_id = get_post($post->ID);
                $title = get_post_meta($post->ID, WPGEO_TITLE_META, true);
                if (empty($title)) {
                    $title = $post_id->post_title;
                }
                if (is_numeric($latitude) && is_numeric($longitude)) {
                    $push = array('id' => $post->ID, 'latitude' => $latitude, 'longitude' => $longitude, 'title' => $title, 'post' => $post);
                    array_push($coords, $push);
                }
            }
            // Markers JS (output)
            $markers_js = '';
            // Only show map widget if there are coords to show
            if (count($coords) > 0) {
                $google_maps_api_key = $wpgeo->get_google_api_key();
                if (!is_numeric($zoom)) {
                    $zoom = $wp_geo_options['default_map_zoom'];
                }
                if (empty($maptype)) {
                    $maptype = empty($wp_geo_options['google_map_type']) ? 'G_NORMAL_MAP' : $wp_geo_options['google_map_type'];
                }
                // Polylines
                $polyline_js = '';
                if ($showpolylines) {
                    $polyline = new WPGeo_Polyline(array('color' => $wp_geo_options['polyline_colour']));
                    for ($i = 0; $i < count($coords); $i++) {
                        $polyline->add_coord($coords[$i]['latitude'], $coords[$i]['longitude']);
                    }
                    $polyline_js = WPGeo_API_GMap2::render_map_overlay('map', WPGeo_API_GMap2::render_polyline($polyline));
                }
                for ($i = 0; $i < count($coords); $i++) {
                    $icon = 'wpgeo_icon_' . apply_filters('wpgeo_marker_icon', 'small', $coords[$i]['post'], 'widget');
                    $markers_js .= 'marker' . $i . ' = wpgeo_createMarker(new GLatLng(' . $coords[$i]['latitude'] . ', ' . $coords[$i]['longitude'] . '), ' . $icon . ', "' . addslashes(__($coords[$i]['title'])) . '", "' . get_permalink($coords[$i]['id']) . '");' . "\n";
                }
                // HTML JS
                $wpgeo->includeGoogleMapsJavaScriptAPI();
                $small_marker = $wpgeo->markers->get_marker_by_id('small');
                $html_js .= '
					<script type="text/javascript">
					//<![CDATA[
					
					/**
					 * Widget Map
					 */
					
					// Define variables
					var map = "";
					var bounds = "";
					
					// Add events to load the map
					GEvent.addDomListener(window, "load", createMapWidget);
					GEvent.addDomListener(window, "unload", GUnload);
					
					// Create the map
					function createMapWidget() {
						if (GBrowserIsCompatible()) {
							map = new GMap2(document.getElementById("' . $id . '"));
							' . WPGeo_API_GMap2::render_map_control('map', 'GSmallZoomControl3D') . '
							map.setCenter(new GLatLng(0, 0), 0);
							map.setMapType(' . $maptype . ');
							bounds = new GLatLngBounds();
							
							// Add the markers	
							' . $markers_js . '
							
							// Draw the polygonal lines between points
							' . $polyline_js . '
							
							// Center the map to show all markers
							var center = bounds.getCenter();
							var zoom = map.getBoundsZoomLevel(bounds)
							if (zoom > ' . $zoom . ') {
								zoom = ' . $zoom . ';
							}
							map.setCenter(center, zoom);
						}
					}
					
					//]]>
					</script>';
                // Set width and height
                if (is_numeric($width)) {
                    $width = $width . 'px';
                }
                if (is_numeric($height)) {
                    $height = $height . 'px';
                }
                $html_js .= '<div class="wp_geo_map" id="' . $id . '" style="width:' . $width . '; height:' . $height . ';"></div>';
            }
            return $html_js;
        }
    }
Beispiel #3
0
/**
 * @method  Get WP Geo Map
 */
function get_wpgeo_map($query, $options = null)
{
    global $wpgeo_map_id;
    $wpgeo_map_id++;
    $id = 'wpgeo_map_id_' . $wpgeo_map_id;
    $wp_geo_options = get_option('wp_geo_options');
    $defaults = array('width' => $wp_geo_options['default_map_width'], 'height' => $wp_geo_options['default_map_height'], 'type' => $wp_geo_options['google_map_type'], 'polylines' => $wp_geo_options['show_polylines'], 'polyline_colour' => $wp_geo_options['polyline_colour'], 'align' => 'none', 'numberposts' => -1, 'post_type' => 'post', 'post_status' => 'publish', 'orderby' => 'post_date', 'order' => 'DESC', 'markers' => 'large', 'markers' => 'large', 'offset' => 0, 'category' => null, 'include' => null, 'exclude' => null, 'meta_key' => null, 'meta_value' => null, 'post_mime_type' => null, 'post_parent' => null);
    // Validate Args
    $r = wp_parse_args($query, $defaults);
    if (is_numeric($r['width'])) {
        $r['width'] .= 'px';
    }
    if (is_numeric($r['height'])) {
        $r['height'] .= 'px';
    }
    $posts = get_posts($r);
    $output = '
		<div id="' . $id . '" class="wpgeo_map" style="width:' . $r['width'] . '; height:' . $r['height'] . ';float:' . $r['align'] . '"></div>
		<script type="text/javascript">
		<!--
		jQuery(window).load( function() {
			if ( GBrowserIsCompatible() ) {
				var bounds = new GLatLngBounds();
				map = new GMap2(document.getElementById("' . $id . '"));
				' . WPGeo_API_GMap2::render_map_control('map', 'GLargeMapControl3D') . '
				map.setMapType(' . $r['type'] . ');
				';
    if ($posts) {
        $polyline = new WPGeo_Polyline(array('color' => $r['polyline_colour']));
        foreach ($posts as $post) {
            $latitude = get_post_meta($post->ID, WPGEO_LATITUDE_META, true);
            $longitude = get_post_meta($post->ID, WPGEO_LONGITUDE_META, true);
            if (is_numeric($latitude) && is_numeric($longitude)) {
                $marker = get_post_meta($post->ID, WPGEO_MARKER_META, true);
                if (empty($marker)) {
                    $marker = $r['markers'];
                }
                $icon = 'wpgeo_icon_' . apply_filters('wpgeo_marker_icon', $marker, $post, 'wpgeo_map');
                $polyline->add_coord($latitude, $longitude);
                $output .= '
					var center = new GLatLng(' . $latitude . ',' . $longitude . ');
					var marker = new wpgeo_createMarker2(map, center, ' . $icon . ', \'' . esc_js($post->post_title) . '\', \'' . get_permalink($post->ID) . '\');
					bounds.extend(center);
					';
            }
        }
        if ($r['polylines'] == 'Y') {
            $output .= WPGeo_API_GMap2::render_map_overlay('map', WPGeo_API_GMap2::render_polyline($polyline));
        }
        $output .= '
			zoom = map.getBoundsZoomLevel(bounds);
			map.setCenter(bounds.getCenter(), zoom);
			';
    } else {
        $output .= '
				map.setCenter(new GLatLng(' . $wp_geo_options['default_map_latitude'] . ', ' . $wp_geo_options['default_map_longitude'] . '), ' . $wp_geo_options['default_map_zoom'] . ');';
    }
    $output .= '
			}
		} );
		-->
		</script>
		';
    return $output;
}
Beispiel #4
0
/**
 * Get WP Geo Map
 *
 * @param array $query Query args.
 * @param array $options Options array.
 * @return string Output.
 */
function get_wpgeo_map($query, $options = null)
{
    global $wpgeo, $wpgeo_map_id;
    $wpgeo_map_id++;
    $id = 'wpgeo_map_id_' . $wpgeo_map_id;
    $wp_geo_options = get_option('wp_geo_options');
    $defaults = apply_filters('wpgeo_map_default_query_args', array('width' => $wp_geo_options['default_map_width'], 'height' => $wp_geo_options['default_map_height'], 'type' => $wp_geo_options['google_map_type'], 'polylines' => $wp_geo_options['show_polylines'], 'polyline_colour' => $wp_geo_options['polyline_colour'], 'zoom' => $wp_geo_options['default_map_zoom'], 'align' => 'none', 'numberposts' => -1, 'posts_per_page' => -1, 'post_type' => 'post', 'post_status' => 'publish', 'orderby' => 'post_date', 'order' => 'DESC', 'markers' => 'large', 'offset' => 0, 'category' => null, 'include' => null, 'exclude' => null, 'meta_key' => null, 'meta_value' => null, 'post_ids' => '', 'post_mime_type' => null, 'post_parent' => null));
    // Validate Args
    $r = wp_parse_args($query, $defaults);
    $r['width'] = wpgeo_css_dimension($r['width']);
    $r['height'] = wpgeo_css_dimension($r['height']);
    if ($r['posts_per_page'] < $r['numberposts']) {
        $r['posts_per_page'] = $r['numberposts'];
    }
    // Set 'post__in' if 'post_ids' set, but don't overwrite.
    if (!empty($r['post_ids']) && empty($r['post__in'])) {
        if (is_array($r['post_ids'])) {
            $r['post__in'] = $r['post_ids'];
        } else {
            $r['post__in'] = explode(',', $r['post_ids']);
        }
    }
    $posts = get_posts($r);
    // Map
    $map = new WPGeo_Map('id_' . $wpgeo_map_id);
    $map->set_size($r['width'], $r['height']);
    $map->set_map_centre(new WPGeo_Coord($wp_geo_options['default_map_latitude'], $wp_geo_options['default_map_longitude']));
    $map->set_map_zoom($r['zoom']);
    $map->set_map_type($r['type']);
    // Points
    if ($posts) {
        foreach ($posts as $post) {
            $coord = get_wpgeo_post_coord($post->ID);
            if ($coord->is_valid_coord()) {
                $marker = get_post_meta($post->ID, WPGEO_MARKER_META, true);
                if (empty($marker)) {
                    $marker = $r['markers'];
                }
                $map->add_point($coord, array('icon' => apply_filters('wpgeo_marker_icon', $marker, $post, 'template'), 'title' => get_wpgeo_title($post->ID), 'link' => apply_filters('wpgeo_marker_link', get_permalink($post), $post), 'post' => $post));
            }
        }
    }
    // Polylines
    if (count($map->points) > 0) {
        if ($r['polylines'] == 'Y') {
            $polyline = new WPGeo_Polyline(array('color' => $r['polyline_colour']));
            foreach ($map->points as $point) {
                $polyline->add_coord($point->get_coord());
            }
            $map->add_polyline($polyline);
        }
    }
    $center_coord = $map->get_map_centre();
    $wpgeo->maps->add_map($map);
    return $map->get_map_html(array('styles' => array('float' => $r['align'])));
}
Beispiel #5
0
    /**
     * @method       Render Map JavaScript
     * @description  Outputs the javascript to display maps.
     * @param        $map_id = The map ID.
     * @return       (string) JavaScript
     */
    function renderMapJS($map_id = false)
    {
        $wp_geo_options = get_option('wp_geo_options');
        // ID of div for map output
        $map_id = $map_id ? $map_id : $this->id;
        $div = 'wp_geo_map_' . $map_id;
        // Map Types
        $maptypes = $this->maptypes;
        $maptypes[] = $this->maptype;
        $maptypes = array_unique($maptypes);
        $js_maptypes = WPGeo_API_GMap2::render_map_types('map_' . $map_id, $maptypes);
        // Markers
        $js_markers = '';
        if (count($this->points) > 0) {
            for ($i = 0; $i < count($this->points); $i++) {
                $icon = 'wpgeo_icon_' . apply_filters('wpgeo_marker_icon', $this->points[$i]['icon'], $this->id, 'wpgeo_map');
                $js_markers .= 'var marker_' . $map_id . '_' . $i . ' = new wpgeo_createMarker2(map_' . $map_id . ', new GLatLng(' . $this->points[$i]['latitude'] . ', ' . $this->points[$i]['longitude'] . '), ' . $icon . ', \'' . addslashes(__($this->points[$i]['title'])) . '\', \'' . $this->points[$i]['link'] . '\');' . "\n";
                $js_markers .= 'bounds.extend(new GLatLng(' . $this->points[$i]['latitude'] . ', ' . $this->points[$i]['longitude'] . '));';
            }
        }
        // Show Polyline
        $js_polyline = '';
        if ($wp_geo_options['show_polylines'] == 'Y') {
            if ($this->show_polyline) {
                if (count($this->points) > 1) {
                    $polyline = new WPGeo_Polyline(array('color' => $wp_geo_options['polyline_colour']));
                    for ($i = 0; $i < count($this->points); $i++) {
                        $polyline->add_coord($this->points[$i]['latitude'], $this->points[$i]['longitude']);
                    }
                    $js_polyline .= WPGeo_API_GMap2::render_map_overlay('map_' . $map_id, WPGeo_API_GMap2::render_polyline($polyline));
                }
            }
        }
        // Zoom
        $js_zoom = '';
        if (count($this->points) > 1) {
            $js_zoom .= 'map_' . $map_id . '.setCenter(bounds.getCenter(), map_' . $map_id . '.getBoundsZoomLevel(bounds));';
        }
        if (count($this->points) == 1) {
            if (is_numeric($this->mapcentre['latitude']) && is_numeric($this->mapcentre['longitude'])) {
                $js_zoom .= 'map_' . $map_id . '.setCenter(new GLatLng(' . $this->mapcentre['latitude'] . ', ' . $this->mapcentre['longitude'] . '));';
            }
        }
        // Controls
        $js_controls = '';
        if ($this->show_map_scale) {
            $js_controls .= WPGeo_API_GMap2::render_map_control('map_' . $map_id, 'GScaleControl');
        }
        if ($this->show_map_overview) {
            $js_controls .= WPGeo_API_GMap2::render_map_control('map_' . $map_id, 'GOverviewMapControl');
        }
        // Map Javascript
        $js = '
			if (document.getElementById("' . $div . '"))
			{
				var bounds = new GLatLngBounds();
    
				map_' . $map_id . ' = new GMap2(document.getElementById("' . $div . '"));
				var center = new GLatLng(' . $this->points[0]['latitude'] . ', ' . $this->points[0]['longitude'] . ');
				map_' . $map_id . '.setCenter(center, ' . $this->zoom . ');
				
				' . $js_maptypes . '
				map_' . $map_id . '.setMapType(' . $this->maptype . ');
				
				' . WPGeo_API_GMap2::render_map_control('map_' . $map_id, 'GMapTypeControl');
        if ($this->mapcontrol != "") {
            $js .= WPGeo_API_GMap2::render_map_control('map_' . $map_id, $this->mapcontrol);
        }
        $js .= '
				var center_' . $map_id . ' = new GLatLng(' . $this->points[0]['latitude'] . ', ' . $this->points[0]['longitude'] . ');
				
				' . apply_filters('wpgeo_map_js_preoverlays', '', 'map_' . $map_id) . '
				
				' . $js_markers . '
				' . $js_polyline . '
    			' . $js_zoom . '
    			' . $js_controls . '
				
				//' . WPGeo_API_GMap2::render_map_overlay('map_' . $map_id, 'new GLayer("org.wikipedia.en")') . ';
				//' . WPGeo_API_GMap2::render_map_overlay('map_' . $map_id, 'new GLayer("com.panoramio.all")') . ';
				//' . WPGeo_API_GMap2::render_map_overlay('map_' . $map_id, 'new google.maps.LocalSearch()') . '; // http://googleajaxsearchapi.blogspot.com/2007/06/local-search-control-for-maps-api.html
				
			}';
        return $js;
    }