You need to have some initial (maybe global) js var markers_array = [];
. Then use an ajax callback function to bump your markers into that array.
Register your scripts
// Define $root_url = plugin_dir_path(); // Example path $ajax_nonce = 'your_nonce'; $ajax_nonce_val = 'your_val'; // Ajax wp_register_script( 'json2', false, false, false, true ); wp_enqueue_script( 'json2' ); wp_enqueue_script( 'ajax-script' ,"{$root_url}js/ajax_json.js" // your ajax callback script that handles the request (inside a /js folder) ,array( 'json2' ) ,false ,true ); wp_localize_script( 'ajax-script' ,'ajax_object' // YOUR MAIN DATA OBJECT ,array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ,$ajax_nonce => wp_create_nonce( $ajax_nonce_val ) ) );The initial Markers
// inside some part: You need the following php function to build your markers array: // This js array will be used by your custom build markers js function to build the initial markers. It can also be used to update markers and overlays. var mapsInitMarkers = ; // Markers builder callback function for init js-var function build_markers_array() { $output = ''; $markers = array( /* DEFINE MARKERS HERE */ ); foreach ( $markers as $marker ) { // id, lat, lng, name $output .= "[ {$result['ID']$marker['ID']}, {$result['lat']$marker['lat']}, {$result['lng']$marker['lng']}, '{$result['name']$marker['name']}', ],"; } $output = substr( $output, 0, -1 ); // get rid of last comma $output = "[{$output}]"; return $output; }The Ajax Callback function
function ajax_cb_fn() { check_ajax_referer( 'referer_name', $ajax_nonce ); // needed if performed some update via some ex. form $output = ''; // do stuff here - example, use build_markers_array(); with some input to update your markers
$response = json_encode( array(
'data' => $output
) );
header( "Content-Type: application/json" );
echo $response;
exit();
}