Skip to content

Instantly share code, notes, and snippets.

@adrienlucas
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrienlucas/9813299 to your computer and use it in GitHub Desktop.
Save adrienlucas/9813299 to your computer and use it in GitHub Desktop.
Wordpress (quick n' dirty) shortcode to display a map and markers.
<?
/*
This shortcode display a google map with some markers on it.
*/
/*
<!--Usage :-->
[amod-map]
Somewhere###<strong></strong><br /><a href="http://foobar.com/"###-25.363882,174.044922
Somewhere else###<strong></strong><br /><a href="http://foobar.com/"###15.549876,131.123489
[/amod-map]
*/
define('AMOD_SC_GOOGLEMAP_SEPARATOR', '###');
function amod_sc_googlemap( $atts , $content = null ) {
$markers = '';
foreach(explode("\n", str_replace("\r", "\n", $content)) as $k=>$place) {
$place = trim($place);
if(!empty($place)) {
list($name, $info, $coord) = explode(AMOD_SC_GOOGLEMAP_SEPARATOR, $place);
$markers .= 'var infowindow'.$k.' = new google.maps.InfoWindow({content: "'.$info.'"});'."\n".
'var marker'.$k.' = new google.maps.Marker({ position: new google.maps.LatLng('.$coord.'), map: map, title: "'.$name.'"});'."\n".
'google.maps.event.addListener(marker'.$k.', "click", function(){ infowindow'.$k.'.open(map, marker'.$k.'); });'."\n\n";
}
}
return '<div id="map-canvas" style="width:430px;height:450px;"/>
<script src="https://maps.googleapis.com/maps/api/js?v=3&amp;sensor=false&amp;language=fr&amp;region=fr" type="text/javascript"></script>
<script type="text/javascript">
var map;
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(10, -10),
zoom: 1
};
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
'.$markers.'
};
google.maps.event.addDomListener(window, "load", initialize);
</script>';
}
add_shortcode( 'amod-map', 'amod_sc_googlemap' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment