Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimizations of Javascript and little bug corrections #30

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions admin/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function register_settings() {
add_settings_field( 'google_api_key', __( 'Google API Key', 'wp-geo' ), array( $this, 'google_api_key_field' ), 'wp_geo_options', 'wpgeo_api' );
add_settings_section( 'wpgeo_general', __( 'General Settings', 'wp-geo' ), array( $this, 'general_settings_section' ), 'wp_geo_options' );
add_settings_field( 'google_map_type', __( 'Map Type', 'wp-geo' ), array( $this, 'google_map_type_field' ), 'wp_geo_options', 'wpgeo_general' );
add_settings_field( 'trigger', __( 'Dynamic Map', 'wp-geo' ), array( $this, 'trigger_field' ), 'wp_geo_options', 'wpgeo_general' );
add_settings_field( 'show_post_map', __( 'Show Post Map', 'wp-geo' ), array( $this, 'show_post_map_field' ), 'wp_geo_options', 'wpgeo_general' );
add_settings_field( 'default_map_location', __( 'Default Map Location', 'wp-geo' ), array( $this, 'default_map_location_field' ), 'wp_geo_options', 'wpgeo_general' );
add_settings_field( 'default_map_width', __( 'Default Map Width', 'wp-geo' ), array( $this, 'default_map_width_field' ), 'wp_geo_options', 'wpgeo_general' );
Expand Down Expand Up @@ -199,6 +200,23 @@ function default_map_controls_field() {
echo wpgeo_checkbox( 'wp_geo_options[show_map_overview]', 'Y', $options['show_map_overview'], false, 'show_map_overview' ) . ' ' . __( 'Show collapsible overview map (in the corner of the map)', 'wp-geo' );
}

/**
* Triggering of the rendering with the Maps API instead of Static Maps API
*/
function trigger_field() {
$options = get_option( 'wp_geo_options' );
$menu_options = array(
'load' => __( 'Always use dynamic maps', 'wp-geo'),
'none' => __( 'Use static pictures to render the maps', 'wp-geo' ),
'click touchstart' => __( 'Use static pictures to render the maps, make it dynamic on mouse click', 'wp-geo' ),
'mouseenter touchstart' => __( 'Use static pictures to render the maps, make it dynamic when the mouse is over', 'wp-geo' ),
);
echo wpgeo_select( 'wp_geo_options[trigger]', $menu_options, $options['trigger'], false, 'trigger' );
}

/**
* Google API Key Field
*/
/**
* Default Post Options Field
*/
Expand Down
120 changes: 14 additions & 106 deletions api/googlemapsv3/googlemapsv3.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ function WPGeo_API_GoogleMapsV3() {
* @return string Marker icon.
*/
function wpgeo_api_googlemapsv3_markericon( $value, $marker ) {
$value = "wpgeo_createIcon(" . $marker->width . ", " . $marker->height . ", " . $marker->anchorX . ", " . $marker->anchorY . ", '" . $marker->image . "', '" . $marker->shadow . "')";
return $value;
return $marker;
}

/**
Expand Down Expand Up @@ -65,121 +64,30 @@ function wpgeo_decode_api_string( $string, $key, $context ) {
}
return $string;
}

function get_markers_js( $map ) {
$markers = '';
for ( $i = 0; $i < count( $map->points ); $i++ ) {
$post_icon = isset( $map->points[$i]->icon ) ? $map->points[$i]->icon : 'small';
$icon = 'wpgeo_icon_' . $post_icon;
if ( isset( $map->points[$i]->args['post'] ) ) {
$icon = 'wpgeo_icon_' . apply_filters( 'wpgeo_marker_icon', $post_icon, $map->points[$i]->args['post'], 'widget' );
}
$markers .= 'var marker_' . $i . '_' . $map->get_js_id() . ' = new google.maps.Marker({ position:new google.maps.LatLng(' . $map->points[$i]->coord->get_delimited() . '), map:' . $map->get_js_id() . ', icon: ' . $icon . ' });' . "\n";
if ( ! empty( $map->points[$i]->link ) ) {
$markers .= 'google.maps.event.addListener(marker_' . $i . '_' . $map->get_js_id() . ', "click", function() {
window.location.href = "' . $map->points[$i]->link . '";
});
';
}
if ( ! empty( $map->points[$i]->title ) ) {
$markers .= '
var tooltip_' . $i . '_' . $map->get_js_id() . ' = new Tooltip(marker_' . $i . '_' . $map->get_js_id() . ', \'' . esc_js( $map->points[$i]->title ) . '\');
google.maps.event.addListener(marker_' . $i . '_' . $map->get_js_id() . ', "mouseover", function() {
tooltip_' . $i . '_' . $map->get_js_id() . '.show();
});
google.maps.event.addListener(marker_' . $i . '_' . $map->get_js_id() . ', "mouseout", function() {
tooltip_' . $i . '_' . $map->get_js_id() . '.hide();
});
';
}
$markers .= 'bounds.extend(new google.maps.LatLng(' . $map->points[$i]->coord->get_delimited() . '));' . "\n";
}
return $markers;
}

function get_polylines_js( $map ) {
$polylines = '';
if ( count( $map->polylines ) > 0 ) {
$count = 1;
foreach ( $map->polylines as $polyline ) {
$polyline_js_3_coords = array();
foreach ( $polyline->coords as $c ) {
$polyline_js_3_coords[] = 'new google.maps.LatLng(' . $c->get_delimited() . ')';
}
$polylines = 'var polyline_' . $count . '_' . $map->get_js_id() . ' = new google.maps.Polyline({
path : [' . implode( ',', $polyline_js_3_coords ) . '],
strokeColor : "' . $polyline->color . '",
strokeOpacity : ' . $polyline->opacity . ',
strokeWeight : ' . $polyline->thickness . ',
geodesic : ' . $polyline->geodesic . ',
map : ' . $map->get_js_id() . '
});';
$count++;
}
}
return $polylines;
}

function get_feeds_js( $map ) {
$feeds = '';
if ( count( $map->feeds ) > 0 ) {
$count = 1;
foreach ( $map->feeds as $feed ) {
$feeds .= '
var kmlLayer_' . $count . ' = new google.maps.KmlLayer({
url : "' . $feed . '",
map : ' . $map->get_js_id() . '
});';
$count++;
}
}
return $feeds;
}


function wpgeo_js( $maps ) {
if ( ! is_array( $maps ) ) {
$maps = array( $maps );
}
if ( count( $maps ) > 0 ) {
echo '
<script type="text/javascript">
//<![CDATA[
function wpgeo_render_maps() {
';
$filters = array();
foreach ( $maps as $map ) {
$center_coord = $map->get_map_centre();
$filters[] = apply_filters( 'wpgeo_map_js_preoverlays', '', $map->get_js_id() );
}
$filters = array_filter($filters);
if(!empty($filters)) {
echo '
if (document.getElementById("' . $map->get_dom_id() . '")) {
var bounds = new google.maps.LatLngBounds();
var mapOptions = {
center : new google.maps.LatLng(' . $center_coord->get_delimited() . '),
zoom : ' . $map->get_map_zoom() . ',
mapTypeId : ' . apply_filters( 'wpgeo_api_string', 'google.maps.MapTypeId.ROADMAP', $map->get_map_type(), 'maptype' ) . ',
mapTypeControl : false, // @todo
streetViewControl : false // @todo
};
' . $map->get_js_id() . ' = new google.maps.Map(document.getElementById("' . $map->get_dom_id() . '"), mapOptions);

// Add the markers and polylines
' . $this->get_markers_js( $map ) . '
' . $this->get_polylines_js( $map ) . '
';
if ( count( $map->points ) > 1 ) {
echo $map->get_js_id() . '.fitBounds(bounds);';
}
echo '
' . apply_filters( 'wpgeo_map_js_preoverlays', '', $map->get_js_id() ) . '
' . $this->get_feeds_js( $map ) . '
<script type="text/javascript">
//<![CDATA[
function wpgeo_render_map_filters() {
';
echo join("\n", $filters);
echo '
}
';
}
echo '
}
google.maps.event.addDomListener(window, "load", wpgeo_render_maps);
//]]>
google.maps.event.addDomListener(window, "load", wpgeo_render_map_filters);
//]]>
</script>';
}
}
}

Expand Down
14 changes: 9 additions & 5 deletions api/googlemapsv3/js/admin-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
*/
function wpgeo_init_admin_post_map() {

$(document).ready(function($) {

// Define map
WPGeo_Admin.map = new google.maps.Map(document.getElementById(WPGeo_Admin.map_dom_id), {
zoom : parseInt(WPGeo_Admin.zoom, 10),
center : new google.maps.LatLng(WPGeo_Admin.mapCentreX, WPGeo_Admin.mapCentreY),
mapTypeId : WPGeo_Admin.mapType,
mapTypeId : eval(WPGeo_Admin.mapType),
zoomControl : true,
mapTypeControl : true
});
Expand Down Expand Up @@ -149,8 +147,14 @@

// Map ready, do other stuff if needed
$("#wpgeo_location").trigger("WPGeo_adminPostMapReady");
});
}
google.maps.event.addDomListener(window, "load", wpgeo_init_admin_post_map);


$(window).load(function() {
// Get the conf stocked in the data of a div
var conf = $('.wpgeo_conf').first().data();
if(conf)
wpgeo_load_api(conf.apiuri).done(wpgeo_init_admin_post_map);
});

})(jQuery);
Loading