yet another blog about computer, technology, programming, and internet

Thursday, February 03, 2011

GWT and Delayed Execution for Google Maps Geocoding

Thursday, February 03, 2011 Posted by Ismail Habib , 25 comments
Google Maps provide us with an asynchronous way of doing a geocoding, which is pretty much how GWT is dealing with all type of client-server communication. However, sometimes we need a way to synchronize them. By synchronizing I mean waiting for the required asynchronous call to return a value before continue with other execution. Using something like Timer to wait for a reply doesn't work since it will only block the whole process on the browser since JavaScript interpreter is single-threaded. Fortunately, there is a way to deal with it within GWT by using DeferredCommand.

I'm going to take an example of using Geocoding from Google Maps. What I would like to do is geocode two address into latitude, longitude coordinate and use them as parameters for another method.

Geocoder geocode = new Geocoder();
final Callback firstCallback = new Callbak();
geocode.getLatLng("Jl. Tb Ismail Bandung Indonesia", firstCallback);
final Callback secondCallback = new Callbak();
geocode.getLatLng("Jl. Ganesha Bandung Indonesia", secondCallback);
final Command command = new Command() {
   @Override
   public void execute(){
      if ((firstCallback.location != null) 
            && (secondCallback.location != null)) {
         //call the method
         anotherMethod(firstCallback.location, secondCallback.location);
      } else {
         DeferredCommand.addCommand(this); //delay execution
      }
   }
}
command.execute();

With "Callback" as a class to temporarily store value from geocode.

class Callback implements LatLngCallback {
   public LatLng location;

   @Override
   public void onFailure() {
      //put something here
   }

   @Override
   public void onSuccess(LatLng point) {
      location = point;
   }
}

This works for me well, however, I do realize that DeferredCommand is now deprecated. Any other solution?

25 comments:

  1. Anonymous3:01 AM

    For a well being treatment practitioner, two issues are of utmost importance though selecting any drug or health supplement for
    a person. The same type of beans are roasted different amounts
    based on the intended characteristics. Jura Coffee machine has all vast range of coffee machines to match with your requirements.



    My web site ... Garcinia HCA

    ReplyDelete
  2. Anonymous4:49 AM

    Instawlling this within your conference spac
    will assist you too with great displays and after a little much more investment you hhave access to a
    killer home theatre system that might be the articular envy of all whom see
    annd leazrn about it. When you go to clean the disc, if you don't
    have any static-free cloths lying around, use a paper towel and spray the solution on the towel.
    Yellow - Top Optima batteries arre recommended for use in vehicles that have a lot of
    accessories, such as audio systems, wincfhes and commercial vehicles.



    my web blog; jackery giant high-capacity premium aluminum portable charger 10400mah external battery

    ReplyDelete
  3. Anonymous11:04 AM

    It's an amazing post in support of all the internet people; they will take advantage from it I am sure.


    my website; ครีมหน้า

    ReplyDelete
  4. Anonymous8:39 PM

    I fοr all time emaԝiled this blog pοst pagе to all mmy assߋciates, since if like tto read it next my links will too.


    my web page no pain

    ReplyDelete
  5. When ordering your designer eyeglasses online, it is very important to make sure that you are choosing a reputable website.
    If you want to buy any Prescription Glasses and Round Sun Glasses. Please visit our Store to buy Best Glasses.
    Best EyeGlasses in US

    ReplyDelete