Instantly share code, notes, and snippets.

Embed
What would you like to do?
Show Markers on the Google Maps Using Remote JSON File
add_filter('wpgmp_marker_source','wpgmp_marker_source',1,2);
function wpgmp_marker_source($markers,$map_id) {
/* You can create array of the markers to add on the google maps dynamically. It's very useful hook if you want to fetch
locations from an external database, API, JSON or XML.
*/
$markers = array();
$json_entries = wp_remote_get('https://www.flippercode.com/sample_markers.json');
$array_entries = json_decode($json_entries['body'], true);
if(!empty($array_entries)) {
// Loop Start Here...
foreach($array_entries as $key => $entry) {
if($key == 0 ) {
continue; //Skip header column in the csv.
}
$marker = array();
$marker['title'] = $entry[0]; // Assign a title to the marker.
$marker['address'] = $entry[1]; // Assign an address to the marker.
$marker['latitude'] =$entry[2]; // Assign latitude to the marker.
$marker['longitude'] =$entry[3]; // Assign longitude to the marker.
$marker['message'] =$entry[4]; // Show Infowindow message on marker click.
$markers[] = $marker;
}
// Loop ended Here...
}
return $markers; // Return array of markers.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment