How to send data from JS to Java? [message #1848560] |
Tue, 07 December 2021 23:05 |
David Lee 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 #1849376 is a reply to message #1849147] |
Fri, 14 January 2022 16:39 |
David Lee 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
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03035 seconds