none
[Bing Map AJAX API 6.3] FindLocations callback function

    Question

  • Hello everyone!!! :-)

    I have this piece of code using Bing Map API 6.3:

    function sync(fn) {
        fn();
    }
    
    function doGeocode() {
        console.log("Calling doGeocode for id_address: " + id_address + " and latlng: " + latlng);
        var ll = latlng;
        mapVE.FindLocations(ll, fillAddress);
        console.log("doGeocode right done");
    }
    
    function fillAddress(locations) {
        var x = id_address;
        console.log("[doGeocode] id_address: " + x);
        myGetElementById("div_row_address"+x).innerHTML = locations[0].Name;
    }
    
    function getTableAddress(lat, lon, idrow)
    {
        id_address=idrow;
        latlng = new VELatLong(lat, lon);
        console.log("id_address: " + id_address + " - latlng: " + latlng);
        sync(doGeocode);
        console.log("exiting getTableAddress");
    }

    but it seems it doesn't really wait till FindLocations's callback is complete. In this case I am filling a table of locations and in a certain point it overwrites always the last line as like as it loses row index of the table. Maybe FindLocations's callback when exits loses completely all variables in memory? Any suggest is appreciate..

    Thanks a lot in advance.

    Cheers,

    Luigi


    Luigi

    Monday, September 23, 2013 7:52 AM

Answers

  • Your issue is due to the asynchronous nature of AJAX calls. When a search is performed the code continues to execute and when a search is completed it fires a callback method similar to how events work. In v6.3 the way to get around this is to make recursive calls to the find method. This has been covered many times in these forums. So much so I wrote a blog post on how to do this 4 years ago here: http://rbrundritt.wordpress.com/2009/03/13/recursive-find-calls/

    All that said, you shouldn't be doing any new development on the Bing Maps v6.3 control. It is very old and hasn't seen an update in over 3 years and the next "Update" be to discontinue it. You should be using the Bing Maps v7 AJAX control. It's a lot faster and has a lot more features.

    One last point, if possible you should geocode all your data ahead of time an store the data. geocoding results on the fly usually makes for a slower application and leaves a lot of room for improvement.


    http://rbrundritt.wordpress.com

    Monday, September 23, 2013 1:13 PM
    Owner
  • You can't pass parameters into the callback. That's why you have to make the calls recursively so that you can make each call one at a time and be able to connect the original request to the response.

    As I mentioned, new development should not happen on v6.3 of Bing Maps. The documentation isn't even online anymore for this API.


    http://rbrundritt.wordpress.com

    Tuesday, September 24, 2013 11:23 AM
    Owner

All replies

  • Your issue is due to the asynchronous nature of AJAX calls. When a search is performed the code continues to execute and when a search is completed it fires a callback method similar to how events work. In v6.3 the way to get around this is to make recursive calls to the find method. This has been covered many times in these forums. So much so I wrote a blog post on how to do this 4 years ago here: http://rbrundritt.wordpress.com/2009/03/13/recursive-find-calls/

    All that said, you shouldn't be doing any new development on the Bing Maps v6.3 control. It is very old and hasn't seen an update in over 3 years and the next "Update" be to discontinue it. You should be using the Bing Maps v7 AJAX control. It's a lot faster and has a lot more features.

    One last point, if possible you should geocode all your data ahead of time an store the data. geocoding results on the fly usually makes for a slower application and leaves a lot of room for improvement.


    http://rbrundritt.wordpress.com

    Monday, September 23, 2013 1:13 PM
    Owner
  • Hello Ricky,

    first thank you for your answer... anyway actually I can't upgrade to 7.0 because no time for now to rewrite all code... I am trying indeed to solve this little issue passing parameters to the callback function as like here:

    function doGeocoding2 (latitude, longitude, current_row) 
    {
    	latlng = new VELatLong(latitude, longitude);
    	mapVE.FindLocations(latlng, function(current_row) {
    		showTableAddress(locations, current_row);
    	});	
    }
    	
    function getTableAddresses2() 
    {
    	$('table[id="table_nuovi_eventi"] > tbody > tr').each(function () {
    		if ($(this).find('div[id^="div_latitude"]').length > 0 ) {
    			var sub_element = $(this).find('div[id^="div_element"]').attr('id');
    			id_address = sub_element.substring(11);
    			doGeocoding2( $(this).find('div[id^="div_latitude"]').html(), $(this).find('div[id^="div_longitude"]').html(), id_address );
    		}
    	});
    }
    
    function showTableAddress(locations, id_address) 
    {
    	myGetElementById("div_row_address"+id_address).innerHTML = locations[0].Name;
    }

    but it says locations is not defined in doGeocoding2() function... maybe the only way to call FindLocations() is to call directly the callback function? 

    Thanks in advance!

    Cheers,

    Luigi



    Luigi

    Tuesday, September 24, 2013 10:53 AM
  • You can't pass parameters into the callback. That's why you have to make the calls recursively so that you can make each call one at a time and be able to connect the original request to the response.

    As I mentioned, new development should not happen on v6.3 of Bing Maps. The documentation isn't even online anymore for this API.


    http://rbrundritt.wordpress.com

    Tuesday, September 24, 2013 11:23 AM
    Owner
  • OK Ricky, thanks for your answer.... I figured out doing some recursive coding and now it works.

    Soon I'll upgrade to 7.0.

    Cheers,

    Luigi


    Luigi

    Tuesday, September 24, 2013 2:12 PM