You have two separate asynchronous events:
- map initializtion on the page load event
- ajax return of data for markers
one option is to create a single function that displays the markers once the map is initialized, call it both at the end of the map initialization and the ajax success function, then the order doesn't matter, when both have occurred the markers will be displayed on the map.
variables in the global scope:
var points = [];
var map = null;
AJAX:
$.ajax({
type:"GET",
dataType:"json",
url:"<?php echo site_url("sandbox_faizan/get_coordinates") ?>",
success: function(result)
{
for(var i=0;i<result.length;i++)
{
points.push(new google.maps.LatLng(result[i].lat,result[i].lng));
}
addMarkers();
}
});
initialization:
function initialize() {
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
addMarkers();
}
google.maps.event.addDomListener(window, 'load', initialize);
add markers to the map:
function addMarkers() {
// when the map is initialized and the points have been initialized, add them to the map
if ((map != null) && (points.length > 0)) {
for (var i = 0; i < points.length; i++) {
var marker = new MarkerWithLabel({
map: map,
position: points[i],
icon: pinImage,
shadow: pinShadow,
labelContent: count,
labelAnchor: new google.maps.Point(12, -5),
labelClass: "labels"
});
}
}
}