/** * 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; } }
/** * 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']))); }