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.
With "Callback" as a class to temporarily store value from geocode.
This works for me well, however, I do realize that DeferredCommand is now deprecated. Any other solution?
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?