Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Remote Application Platform (RAP) » How to send data from JS to Java?
How to send data from JS to Java? [message #1848560] Tue, 07 December 2021 23:05 Go to next message
David Lee is currently offline David LeeFriend
Messages: 81
Registered: May 2013
Member
Hi,

I followed https://eclipsesource.com/blogs/2010/12/18/a-new-google-maps-widget-for-swt-and-rap/
, it works very well without using RemoteObject!

As "You simply call JavaScript from Java and vice versa!" stated on the article ,
I tried to send latitude/longitude data from GMap.js to GMap.java by adding the following code in GMap.js.

window.getCurrentPosition = function() {
navigator.geolocation.getCurrentPosition(position => {
const { latitude, longitude } = position.coords; // successfully get latitude/longitude

// pack data in JSON form.
var jsonStr = '{"latitude":' + latitude.toString() + ', ' + '"longitude":' + longitude.toString() + '}';
const jsonCenter = JSON.parse(jsonStr);

return jsonCenter; // send back to RAP
});
}

Calling the function above from GMap.java with no error, but cannot get the return value, always gets null on Java side.
How can I achieve that?

Thank you so much!

David

[Updated on: Mon, 17 January 2022 01:52]

Report message to a moderator

Re: How to send data from JS to Java? [message #1849147 is a reply to message #1848560] Wed, 05 January 2022 12:00 Go to previous messageGo to next message
Ivan Furnadjiev is currently offline Ivan FurnadjievFriend
Messages: 2427
Registered: July 2009
Location: Sofia, Bulgaria
Senior Member
Hi,

how do you call this function from the server? Is it possible to provide a complete example.

Regards,
Ivan
Re: How to send data from JS to Java? [message #1849376 is a reply to message #1849147] Fri, 14 January 2022 16:39 Go to previous messageGo to next message
David Lee is currently offline David LeeFriend
Messages: 81
Registered: May 2013
Member
Hi, Ivan,

I don't know much of JavaScript, completely followed and based on Mr. Tim Buschtöns's example.
I finally changed it as below.

JavaScript:
window.currentlatlng = '';
window.getCurrentPosition = function() {
navigator.geolocation.getCurrentPosition(position => {
const { latitude, longitude } = position.coords; // successfully get latitude/longitude
let latlng = latitude.toString() + ', ' + longitude.toString();
window.currentlatlng = latlng;});
}
window.getCurrentLatLng = function() {
return window.currentlatlng;
}

Java:
browser.evaluate( "getCurrentPosition()" );
String currentLatLng = (String) browser.evaluate( "return getCurrentLatLng()" ); // successfully gets value

BTW, another question is the error as following.
for example:
navigator.geolocation.getCurrentPosition(position => {
const { latitude, longitude } = position.coords; // OK
...
window.geocoder = new google.maps.Geocoder(); // error here
});

How to access GMap API like 'new google.maps.Geocoder()' inside navigator.geolocation.getCurrentPosition() function?
Thank you and have a nice day.

David

[Updated on: Mon, 17 January 2022 02:48]

Report message to a moderator

Re: How to send data from JS to Java? [message #1856192 is a reply to message #1849376] Fri, 25 November 2022 11:13 Go to previous messageGo to next message
Julien Guigné is currently offline Julien GuignéFriend
Messages: 9
Registered: April 2022
Junior Member
Hi,

you should implement a BrowserCallback in your browser.evaluate

https://download.eclipse.org/rt/rap/doc/3.22/guide/reference/api/org/eclipse/swt/browser/Browser.html#evaluate(java.lang.String,org.eclipse.rap.rwt.widgets.BrowserCallback)

And your getCurrentPosition() should return a String rather than a Json.

In your callback, evaluationSucceeded handles the return of "getCurrentPosition()" as Object.
Cast the result if instance of String and parse it.

Regards.
Re: How to send data from JS to Java? [message #1856197 is a reply to message #1856192] Fri, 25 November 2022 22:21 Go to previous message
David Lee is currently offline David LeeFriend
Messages: 81
Registered: May 2013
Member
Hi, Julien

Thank you for your answer!


David
Previous Topic:Menu issues
Next Topic: Remote Object - instant variable transmittion of both sides?
Goto Forum:
  


Current Time: Sat Sep 21 05:24:07 GMT 2024

Powered by FUDForum. Page generated in 0.03035 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top