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?