Skip to content

Instantly share code, notes, and snippets.

@halfempty
Created September 28, 2012 02:01
Show Gist options
  • Save halfempty/3797570 to your computer and use it in GitHub Desktop.
Save halfempty/3797570 to your computer and use it in GitHub Desktop.
Centering a Google Map on the Latest WordPress Post
<script type="text/javascript" src="<?php echo get_stylesheet_directory_uri() ?>/map.js"></script>
<script type="text/javascript">
var infowindow = new google.maps.InfoWindow();
var pinkmarker = new google.maps.MarkerImage('/wp-content/themes/mapdemo/pink_Marker.png', new google.maps.Size(20, 34) );
var shadow = new google.maps.MarkerImage('/wp-content/themes/mapdemo/shadow.png', new google.maps.Size(37, 34) );
function initialize() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: new google.maps.LatLng(38.898748, -77.037684),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
for (var i = 0; i < locations.length; i++) {
var marker = new google.maps.Marker({
position: locations[i].latlng,
icon: pinkmarker,
shadow: shadow,
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i].info);
infowindow.open(map, marker);
}
})(marker, i));
}
}
</script>
<?php
$the_query = new WP_Query('showposts=1');
while ( $the_query->have_posts() ) : $the_query->the_post();
$coordinates = get_geocode_latlng($post->ID);
endwhile;
?>
center: new google.maps.LatLng(38.898748, -77.037684),
center: new google.maps.LatLng<?php echo $coordinates ?>,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment