[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Edlug Archive Jun 2009 ]

[edlug] Google Maps API Problem




Hi,


I'm working on a wee project that will use Google Maps, my apologies if my examples seem contrived or obfuscated. I'm having some problems with some geocoding and was hoping that someone on list might be have some thoughts...

So, user enters postcode. I do an AJAX request to a homegrown API that returns a list of addresses as XML, including postcodes _associated_ with the user-entered postcode.

Due to inaccuracies in the Google GClientGeocoder object (grrr Royal Mail), I have instead used the GLocalSearch object, and parsed out the first result (which is a more accurate Lng/Lat point).

// AJAX call, iterate over resulting postcodes, and call
// usePointFromPostcode with a callback to add a marker.
[code]
...
var url="/api/eh52gj";
new Ajax.Request(url, {
  method: 'get',
  onSuccess: function(transport) {
    var addrs = transport.responseXML.getElementsByTagName('addrs');
    for (var i = 0; i < stores.length; i++) {
      var postcode =
      addrs[i].getElementsByTagName('postcode')[0]...
        .childNodes[0].nodeValue;
      usePointFromPostcode(postcode, placeMarkerAtPoint);
    }
  }
});
...
[/code]

// This function _geocodes_ a postcode using LocalSearch, and if as
// above, the placeMarkerAtPoint callback is given, a marker ovarlay
// is placed on the map.
[code]
function usePointFromPostcode(postcode, callbackFunction) {
  localSearch = new google.search.LocalSearch();
  localSearch.setSearchCompleteCallback(null, function() {
    if (localSearch.results[0]) {
      var resultLat = localSearch.results[0].lat;
      var resultLng = localSearch.results[0].lng;
      var point = new GLatLng(resultLat,resultLng);
      callbackFunction(point);
      map.panTo(point);
    }
  });

  localSearch.execute(postcode + ", UK");
}
[/code]

I believe that the call to localSearch.execute is performed asynchronously, and that the callback closure is only called when the search has completed. I further believe that this is causing only the first and last postcode in the initial loop to be successfully geocoded.

Does anyone know how to serialise such calls, or detect when the search is completed such that I only call usePointFromPostcode in loop k+1 if the call in loop k has completed?

Many Thanks,
Max
--
Max Manders
max@xxx.xxx.xxx
http://maxmanders.co.uk
-
----------------------------------------------------------------------
You can find the EdLUG mailing list FAQ list at:
http://www.edlug.org.uk/list_faq.html



This archive is kept by wibble+RM@xxx.xxx.xxx
Morpheux
HomePage