Google Maps API
-
I wanted to make some code available for someone to improve on…
I have a wordpress site where I aggregate regional news. When Google Maps API code became available I was eager to try to incorporate it into my site which is what I did earlier this summer. I have a category called “Regional News” and I put embedded a news map into the archive page.
This requires the GEO plugin as well as a valid Google Maps API key for the webpage of course. Here’s the code:
<?php get_header(); ?>
<script src=”https://maps.google.com/maps?file=api&v=1&key=%5Byour site google maps api key]” type=”text/javascript”></script><div id=”content” class=”narrowcolumn”>
<h3>News Map</h3>
<div id=”map” style=”width: 400px; height: 400px”></div><script type=”text/javascript”>
//<![CDATA[
// Creates a marker whose info window displays the given number
function createMarker(point, text) {
var marker = new GMarker(point);// Show this marker’s index in the info window when it is clicked
var html = text;
GEvent.addListener(marker, “click”, function() {
marker.openInfoWindowHtml(html);
});return marker;
}
function markerClicked(marker,html)
{
if((map.infoWindow)&&map.infoWindow.isVisible())
{
map.closeInfoWindow();
}
else
marker.openInfoWindowHtml(html);
}var panel = document.getElementById(‘panel’);
var CategoryCurr = “”;
var CategoryNew = “”;
var html = “”;var map = new GMap(document.getElementById(“map”));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
map.centerAndZoom(new GPoint(-76.112672, 43.065321), 9);
</script>
<?php while (have_posts()) : the_post(); ?>
<?php if ( !(in_category(’22’)) ) { ?>
<?php $lon = get_lon(); ?>
<?php $lat = get_lat(); ?>
<?php $info1 = “
<?php $link = get_permalink() ?>
<?php $title = the_title(”,”,false); ?>
<?php $postdate = get_the_time(‘m/j/y’) ?>
<?php
$postdata = $wpdb->get_row(“SELECT post_excerpt FROM wp_posts WHERE id = $id”);
?><script type=”text/javascript”>
var point = new GPoint(<?php echo $lon; ?>,<?php echo $lat; ?>);
var post_html = “<small><b>Date: ” + ‘<?php echo $postdate; ?>’+ “</b></small><br><b>”+”‘ + “‘> ” +
‘<?php echo $title; ?>’+ “</b><br>” + ‘<?php echo $postdata->post_excerpt; ?>’;
var marker = createMarker(point, post_html);
map.addOverlay(marker);
</script>
<?php } ?> <!– Close the if statement. –>
<?php endwhile; ?>
<?php rewind_posts(); ?>
- The topic ‘Google Maps API’ is closed to new replies.