GMAP - Muiltiple Markers
by bizamajig
HTML
<script type="text/javascript"
src="http://maps.googleapis.com/maps/api/js?sensor=false">
</script>
<div id="gigpress-map" style="width:600px; height:400px"></div>
JavaScript
// Accepts an array of gigs
function initializeMap(gigs) {
var arraySize = gigs.length;
var geocodedAlready = 0;
var markers = gigs;
// Create geocoder
var geocoder = new google.maps.Geocoder();
// Create the map
var map = new google.maps.Map(document.getElementById("gigpress-map"), {
mapTypeId: google.maps.MapTypeId.ROADMAP
});
// Create infowindow
var infowindow = new google.maps.InfoWindow({
content: "holding..."
});
// Create boundary
var bounds = new google.maps.LatLngBounds();
// Add markers to map
for (index in markers) addMarker(markers[index]);
function addMarker(data) {
var address = [data.address, data.city, data.state, data.zip ].join(', ');
// Geocoder
geocoder.geocode({
'address': address
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var marker = new google.maps.Marker({
position: results[0].geometry.location,
map: map,
title: data.venue
});
// Add to boundary
bounds.extend(results[0].geometry.location);
// Bind click event to each marker
google.maps.event.addListener(marker, "click", function() {
infowindow.open(map, this);
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
geocodedAlready++;
if (geocodedAlready >= arraySize) {
map.fitBounds(bounds);
}
});
}
// Zoom and center the map to fit the markers
//map.fitBounds(bounds);
}
$(document).ready(function() {
initializeMap([
{
date: '08/11/2011',
venue: 'Bizamajig',
address: '56 college st.',
city: 'Asheville',
state: 'NC',
...