I am making a store locator app for mobile devices. A DB query runs and gets distances and lan, lat values etc and shows the nearest stores on a page.
Then the user has the ability to click “view on map” to view the store and the current location on a google map. Heres main part of the code which is from the ajax() success callback:
success: function(result){
var rowCount = result.name.length;
if(rowCount <= 0){
$('span.locatorResults').html("There were no stores found within your specified radius.");
}else{
$( '#storeLocatorMapDisplay' ).live( 'pageshow',function(event){
initializeMapAll(); //This initialise SHOULD be called while the map_canvas div is in the DOM. This is why we have to do hacky workaround of resizing etc..
});
$('span.locatorResults').html("There are " + rowCount + " results within a " + result.radius + " mile radius of your current location:<br /><br />");
for (var i = 0; i < rowCount; i++) {
var storelatlng = new google.maps.LatLng(
parseFloat(result.storeLat[i]),
parseFloat(result.storeLon[i])
);
$( '#storeLocatorMapDisplay' ).live( 'pageshow',function(event){
createMarkerAll(storelatlng, result.name[i], result.address[i]);
});
}
$( '#storeLocatorMapDisplay' ).live( 'pageshow',function(event){
createMarkerCurrentLocation(currentlatlng);
});
}
}
My problem was, I was getting lots of grey padding around the map area and I read the its because the map was getting initialised BEFORE the map_canvas div is loaded into the DOM.
So I decided to initialise the map AND the markers when the map page was loaded, but this requires a lot of .live(‘pageshow’) events.
My question is… is there an easier way to initialise the map before creating the markers and before the map canvas is loaded into the DOM??? Bearing in mind that the markers (as far as i know) have to ge generated in the success callback from the ajax request.
Thanks for your time 🙂
The following example works exactly as you want. The markers are created inside the AJAX success function. For testing reasons I created a cityList array with the latitudes, longitutes. This cityList array should be removed and the data should be retrieved from the AJAX response data.