/** * @method Render * @description Outputs the javascript to display the maps. */ function render() { if (count($this->maps) > 0) { echo ' <script type="text/javascript"> function renderWPGeo() { if (GBrowserIsCompatible()) { '; foreach ($this->maps as $map) { echo ' map = new GMap2(document.getElementById("wpgeo-' . $map['id'] . '")); map.setCenter(new GLatLng(41.875696,-87.624207), 3); geoXml = new GGeoXml("' . $map['rss'] . '"); GEvent.addListener(geoXml, "load", function() { geoXml.gotoDefaultViewport(map); }); ' . WPGeo_API_GMap2::render_map_overlay('map', 'geoXml') . ' '; } echo '} } if (document.all&&window.attachEvent) { // IE-Win window.attachEvent("onload", function () { renderWPGeo(); }); window.attachEvent("onunload", GUnload); } else if (window.addEventListener) { // Others window.addEventListener("load", function () { renderWPGeo(); }, false); window.addEventListener("unload", GUnload, false); } </script> '; } }
/** * @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; } }
/** * @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; }
/** * @method Map Scripts Init * @description Output Javascripts to display maps. * @param $latitude = Latitude * @param $longitude = Longitude * @param $zoom = Zoom * @param $panel_open = Admin panel open? * @param $hide_marker = Hide marker? * @return (string) HTML content */ function mapScriptsInit($latitude, $longitude, $zoom = 5, $panel_open = false, $hide_marker = false) { global $wpgeo, $post; $wp_geo_options = get_option('wp_geo_options'); $maptype = empty($wp_geo_options['google_map_type']) ? 'G_NORMAL_MAP' : $wp_geo_options['google_map_type']; // Centre on London if (!is_numeric($latitude) || !is_numeric($longitude)) { $latitude = $wp_geo_options['default_map_latitude']; $longitude = $wp_geo_options['default_map_longitude']; $zoom = $wp_geo_options['default_map_zoom']; // Default 5; $panel_open = true; $hide_marker = true; } $mapcentre = array($latitude, $longitude); if (is_numeric($post->ID) && $post->ID > 0) { $settings = get_post_meta($post->ID, WPGEO_MAP_SETTINGS_META, true); if (isset($settings['zoom']) && is_numeric($settings['zoom'])) { $zoom = $settings['zoom']; } if (!empty($settings['type'])) { $maptype = $settings['type']; } if (!empty($settings['centre'])) { $new_mapcentre = explode(',', $settings['centre']); if (is_numeric($new_mapcentre[0]) && is_numeric($new_mapcentre[1])) { $mapcentre = $new_mapcentre; } } } // Vars $google_maps_api_key = $wpgeo->get_google_api_key(); $panel_open ? $panel_open = 'jQuery(\'#wpgeolocationdiv.postbox h3\').click();' : ($panel_open = ''); $hide_marker ? $hide_marker = 'marker.hide();' : ($hide_marker = ''); // Script $wpgeo->includeGoogleMapsJavaScriptAPI(); $html_content = ' <script type="text/javascript"> //<![CDATA[ function init_wp_geo_map_admin() { if (GBrowserIsCompatible() && document.getElementById("wp_geo_map")) { map = new GMap2(document.getElementById("wp_geo_map")); var center = new GLatLng(' . $mapcentre[0] . ', ' . $mapcentre[1] . '); var point = new GLatLng(' . $latitude . ', ' . $longitude . '); map.setCenter(center, ' . $zoom . '); map.addMapType(G_PHYSICAL_MAP); var zoom_setting = document.getElementById("wpgeo_map_settings_zoom"); zoom_setting.value = ' . $zoom . '; // Map Controls ' . WPGeo_API_GMap2::render_map_control('map', 'GLargeMapControl3D') . ' ' . WPGeo_API_GMap2::render_map_control('map', 'GMapTypeControl') . ' //map.setUIToDefault(); map.setMapType(' . $maptype . '); var type_setting = document.getElementById("wpgeo_map_settings_type"); type_setting.value = wpgeo_getMapTypeContentFromUrlArg(map.getCurrentMapType().getUrlArg()); GEvent.addListener(map, "click", function(overlay, latlng) { var latField = document.getElementById("wp_geo_latitude"); var lngField = document.getElementById("wp_geo_longitude"); latField.value = latlng.lat(); lngField.value = latlng.lng(); marker.setPoint(latlng); marker.show(); }); GEvent.addListener(map, "maptypechanged", function() { var type_setting = document.getElementById("wpgeo_map_settings_type"); type_setting.value = wpgeo_getMapTypeContentFromUrlArg(map.getCurrentMapType().getUrlArg()); }); GEvent.addListener(map, "zoomend", function(oldLevel, newLevel) { var zoom_setting = document.getElementById("wpgeo_map_settings_zoom"); zoom_setting.value = newLevel; }); GEvent.addListener(map, "moveend", function() { var center = this.getCenter(); var centre_setting = document.getElementById("wpgeo_map_settings_centre"); centre_setting.value = center.lat() + "," + center.lng(); }); marker = new GMarker(point, {draggable: true}); GEvent.addListener(marker, "dragstart", function() { map.closeInfoWindow(); }); GEvent.addListener(marker, "dragend", function() { var coords = marker.getLatLng(); var latField = document.getElementById("wp_geo_latitude"); var lngField = document.getElementById("wp_geo_longitude"); latField.value = coords.lat(); lngField.value = coords.lng(); }); ' . apply_filters('wpgeo_map_js_preoverlays', '', 'map') . ' ' . WPGeo_API_GMap2::render_map_overlay('map', 'marker') . ' ' . $panel_open . ' var latField = document.getElementById("wp_geo_latitude"); var lngField = document.getElementById("wp_geo_longitude"); ' . $hide_marker . ' } } jQuery(window).load( init_wp_geo_map_admin ); jQuery(window).unload( GUnload ); //]]> </script>'; return $html_content; }
function render_polyline($polyline) { // Coords $coords = array(); foreach ($polyline->coords as $coord) { $coords[] = WPGeo_API_GMap2::render_coord($coord); } // Options $options = array(); if ($polyline->geodesic) { $options[] = 'geodesic:true'; } return 'new GPolyline([' . implode(',', $coords) . '],"' . $polyline->color . '",' . $polyline->thickness . ',' . $polyline->opacity . ',{' . implode(',', $options) . '})'; }