0

I am developing a Pure Java program that takes an Address in the form of text and the passes it to Google Map API for further breaking in Country,State and Zip.

But I got stuck somewhere where my Java Program is throwing an exception resulting in awkward results.

Now I decided to use the java integrating it with Rhino. This is because I successfully developed a Web Page that takes a string and parses it in Country,State and Zip. So now my thinking is that I will integrate this JavaScript file in my Java file using Rhino.

           function showLocation(address) {
                var geocoder;
                if(!geocoder){
                    alert("GC is initialised");
                    geocoder = new GClientGeocoder();
                }

                geocoder.getLocations(address , function(response)
                     {
                        if (!response || response.Status.code != 200) {alert("Sorry, unable to locate that address");}
                        else {
                          place = response.Placemark[0];
                        }
                     }//END OF CALLBACK METHOD
                );//END OF GETLOCATIONS
           }//end of showLocation

Now my question is that how can I return the object of place when I call showLocations()

4
  • You're using Javascript running in a Rhino Javascript interpreter embedded in a Java application because you had some problem with the Java application? Wouldn't it be better to solve your Java problem than to introduce more complexity? Commented Nov 29, 2011 at 7:35
  • You are 100% right @FrancisAvila but I tried almost everything that I kow but still that exception persists.So I changed the scenario and decided to use Rhino. Commented Nov 29, 2011 at 7:44
  • You should ask that question on StackOverflow. It doesn't seem that you have. Commented Nov 29, 2011 at 7:50
  • Ok @FrancisAvila I will definitely do that too,Thanks again for your great info. Commented Nov 29, 2011 at 7:53

1 Answer 1

2

You can't! Since getLocations is asynchronous, showLocation has already finished executing before response is available.

You need to accept a callback to showLocation. There are some other problems which I will fix below.

function showLocation(address, callback) {
    var geocoder = new GClientGeocoder(); // no need for !geocoder test--it will always be undefined!

    geocoder.getLocations(address , function(response) {
        if (!response || response.Status.code != 200) {
            alert("Sorry, unable to locate that address");
        } else {
            // you forgot the "var" before "place"--you were making a global variable "place"
            var place = response.Placemark[0];
            if (callback) callback(place);
        }
    });
}

Then you make the user supply a callback like so:

function placeCallback(place) {
    // do something with place object here
}
showLocation('my address', placeCallback);
Sign up to request clarification or add additional context in comments.

6 Comments

So this means I can't return an object from this concept and use it with Rhino?
You can return anything you want from showLocation(), but you can't return something that doesn't exist yet!
So in case if I use your concept of callBack with showLocation('myAddress',placeCallBack), how do I get the returned Object because I will call showLocation('ABC','placeCallBack') from my JAVA code and it in turns call placeCallBack which returns my Object ?
You should create a new question to answer that, because this gets in to your Rhino embedding environment which you don't describe at all. Looking at the Rhino Embedding Tutorial it seems you have at least two options: expose the Java object that wants place to Javascript and invoke it in your placeCallback, or examine the scope object after evaluating your script.
However, I really think you should step back and evaluate why you are embedding a Javascript interpreter in the first place. Maybe you could solve your original Java exception problem and avoid the need for Rhino?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.