2

I have a JavaFX application that shows the location point on Google maps. It is working fine when I pass only variables in javascript. Hhere is the code in JavaFX where I am passing the variables name (lat and lon)

  public void handle(ActionEvent arg0) {
                lat = Double.parseDouble(latitude.getText());
                lon = Double.parseDouble(longitude.getText());

                System.out.printf("%.2f %.2f%n", lat, lon);

                webEngine.executeScript("" +
                    "window.lat = " + lat + ";" +
                    "window.lon = " + lon + ";" +
                    "document.goToLocation(window.lat, window.lon);"
                );
            }
        });

I want to pass a double array instead of double variable. Here is the javascript function where I am receiving values from Java variable name (x and y)

 document.goToLocations = function(x, y) {
        alert("goToLocation, x: " + x +", y:" + y);
        var latLng = new google.maps.LatLng(x, y);
        marker.setPosition(latLng);
        map.setCenter(latLng);


    }

And here is the code example link that i am using Code Example

4
  • 1
    (JavaScript is one word) Commented Apr 18, 2020 at 17:42
  • A solution that came to my mind (I'm not a java developer) is that you can convert the array to string like "[1,2,3,4]" and pass it as a string inside javascript eval() method and assign it to a variable there. Commented Apr 18, 2020 at 17:43
  • typo error sorry Commented Apr 18, 2020 at 17:43
  • (it's ok, but it's good to know) Commented Apr 18, 2020 at 17:44

1 Answer 1

1

You can do something like:

public void handle(ActionEvent arg0) {
    List<Double> list = List.of(Double.parseDouble(latitude.getText()), Double.parseDouble(longitude.getText()));
    //Pass list.toString() to the JavaScript function
    //...
}

document.goToLocations = function(values) {
    var x = values.split(", ");
    alert(x);
    alert(x[0]);
    //...
}

List.of was added in Java9. For Java version lower than 9, use the following statement:

List<Double> list = Arrays.asList(Double.parseDouble(latitude.getText()), Double.parseDouble(longitude.getText()));

-OR-

List<Double> list = new ArrayList<Double>();
list.add(Double.parseDouble(latitude.getText()));
list.add(Double.parseDouble(longitude.getText()));
Sign up to request clarification or add additional context in comments.

10 Comments

The method of(double, double) is undefined for the type List
List.of was added in Java9. I've updated my answer. Feel free to comment in case of any further issue.
thanks for your answer but google maps stop working when i am adding eval function
I've updated my answer with another way instead of using eval. Feel free to comment in case of any further issue.
thank you Arvind Kumar Avinash , i have modify your code and it works now and your answer helped me a lot thank you
|

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.