Search code examples
javascriptgoogle-mapscallbackgoogle-maps-api-2

why my program first executes function addmarkers() - not addAddresstoMap()


I have a function LoadXML() in which after a ajax call to the server I've got an array with 100 addresses type string;

Also here we call the function extractAddressed()with the function addmarkers as callback;

function loadXMLDoc(l)
    {    
      //making ajax call to the server to get the addresses

    arrayMyAddresses.push(rec[i][8] + "," + rec[i][10]);

    extractAddresses(addmarkerss);

    }

Here is extractAddresses function which send a part of the addresses which i want to geocode using the getLocations method which performs asynchronous call to the google servers

function extractAddresses(callback)
    {    alert (startman);
          i=startman;

        while  (i<startman+intPageSize)
        {
            geocoder = new GClientGeocoder();
            //alert(arrayMyAddresses[i]);
            geocoder.getLocations(arrayMyAddresses[i], addAddressToMap);
            i++;

        }

        //callback();
        setTimeout(callback,300);


    }

Here you may also see the function addAddresstoMap which handle the response from google servers and creates an array of gmarkers!

function addAddressToMap(response) 
    {

        if (!response || response.Status.code != 200) 

        {
           alert("Sorry, we were unable to geocode that address");
        } 

        else 
        {   

            place = response.Placemark[0];
            point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
            var marker = new GMarker(point);
            batch.push(marker);
            //alert ("batchaddaddresstomap " + batch.length);

        } 


    }           

And at last addmarkerss() function which add the array of Gmarkers to the map;

function addmarkerss()
    {

        // alert("batchf: " + batch.length);         
         mgr = new MarkerManager(map);
         mgr.addMarkers(batch,13);
         mgr.refresh();

    }

So this is how the programme should work when i call extractAddresses and i give one by one the addresses for geocoding to addAddresstoMap function and they are dynamically added to an array of type Gmarker; when I'm ready with this i call addmarkers() to put the markers on my map;

But what does it make actually

when i call extractaddresses it somehow goes first to addmarkers function it sees that banch array is empty and it doesn't load any markers; When i put setTimeout(callback,300);and i postpone the execution of addmarkers everything works.


What may be the reason. And i also want to ask if the problem is here: Although it is true that a callback function will execute last if it is placed last in the function, this will not always appear to happen. For example, if the function included some kind of asynchronous execution (*like an Ajax call as it's my case), then the callback would execute after the asynchronous action begins, but possibly before it finishes. is it my case? also how can i solve my problem without settimeout*


Solution

  • If the function included some kind of asynchronous execution (like an Ajax call as it's my case), then the callback would execute after the asynchronous action begins, but possibly before it finishes. is it my case?

    It is the case, geocoding is an asynchronous process.

    You must call addmarkerss() inside addAddressToMap() when batch reaches the expected length(the last geocoding-request has been finished)