I need to pass a plugin's $atts variable to a javascript function, but I'm not sure how to do it. The javascript function needs the shortcode's php latitude variable in order to build the map.
My plugin looks like this:
<?php
/*
Plugin Name: Lax Google Map Plugin
Plugin URI: http://www.mysite.com
Description: Makes Map
Version: 1.0
Author URI: http://www.mysite.com
*/
function lax_google_map_init() {
wp_enqueue_script('google-maps', 'http://maps.googleapis.com/maps/api/js?sensor=false');
wp_enqueue_script('lax_google_map_script', plugins_url('js/lax_google_map_script.js', __FILE__), array('google-maps','jquery'));
}
add_action('init', 'lax_google_map_init');
function lax_google_map_maker($atts,$content=null) {
$atts = shortcode_atts( array(
'width' => '600',
'height'=>'600',
'latitude'=>'38.9205'
),
$atts);
$output .= '<div id="map_canvas" style="width:'. $atts['width'] .'px; height: '. $atts['height'] .'px; border:1px solid black;"></div>';
return $output;
}
add_shortcode('lax-google-map', 'lax_google_map_maker');
?>
The javascript file looks like:
jQuery.noConflict();
jQuery(document).ready(function($) {
var latlng = new google.maps.LatLng(39.73, -76.02);
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
});
So I need to replace '39.73' with something like:
pseudo-code:
var latlng = new google.maps.LatLng( + $atts['latitude'] + , -76.02);
Also, the javascript gets loaded before the shortcode $atts are available. What's the best way to change it?
Sorry, for the basic questions. I'm learning how to write a plugin.
Any suggestions?
Thank you.
-Laxmidi
UPDATE:
If I add this:
$params = array (
'latitude'=> '39.01',
'longitude'=> '-76.02'
);
wp_localize_script('lax_google_map_script', 'lax_map_params', $params);
to lax_google_map_init()
And I add:
var latlng = new google.maps.LatLng( lax_map_params.latitude, csf_crime_map_params.longitude);
to my javascript function, the map works. How would I replace '39.01' with the $atts[latitude]
and -76.02 with $atts[longitude]
? I'm still learning about scope in php. Thank you.
for ( var i=0; i < mapsInitMarkers.length; i++ ) { createMarkerFunction( /* put markersInitArray[ i and other values ] here */ ); }