I have managed to solve. Here is the code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/src/markerclusterer.js"></script>
<script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
$(document).ready(function(){
var coordinates, parsedCoordinates;
var map;
var marker;
var markers = [];
var clusterer;
var bounds = new google.maps.LatLngBounds();
var coords = new google.maps.LatLng(0, 0);
var infowindow = new google.maps.InfoWindow();
map_initialize(); // load map
function map_initialize(){
//Google map option
var googleMapOptions =
{
center: coords,
zoom: 0, //zoom level, 0 = earth view to higher value
panControl: true, //enable pan Control
zoomControl: true, //enable zoom control
scaleControl: true, // enable scale control
mapTypeId: google.maps.MapTypeId.ROADMAP // google map type
};
map = new google.maps.Map(document.getElementById("map_container"), googleMapOptions);
// Fit these bounds to the map
map.fitBounds(bounds);
// Marker Clusterer setup
var mcOptions = {
gridSize : 50,
maxZoom : 15
};
}
$(':submit').on('click', function() { // This event fires when a button is clicked
$.ajax({ // ajax call starts
url: 'allCoordinates.php', // JQuery loads allCoordinates.php
dataType: 'json', // Choosing a JSON datatype
success: function(data) // Variable data contains the data we get from serverside
{
for (i = 0; i < data.length; i++) {
bounds.extend(new google.maps.LatLng(data[i].latitude, data[i].longitude));
marker = new google.maps.Marker({
position: new google.maps.LatLng(data[i].latitude, data[i].longitude),
map: map,
icon: 'http://maps.google.com/mapfiles/ms/icons/red.png'
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent("Test");
infowindow.open(map, marker);
}
})(marker, i));
markers.push(marker);
}
// Fit these bounds to the map
map.fitBounds(bounds);
clusterer = new MarkerClusterer(map, markers);
}
});
return false; // keeps the page from not refreshing
});
});
</script>