我正在使用Java Swing中的jxBrowser
开发一个地图,问题是我需要将java / java(executeJavaScriptAndReturnValue)中的lat / long变量返回到java Swing。
browser.executeJavaScriptAndReturnValue(
"map.addListener('click', function(e) {\n" +
"placeMarker(e.latLng, map);\n" +
"});\n" +
"\n" +
"function placeMarker(position, map) {\n" +
" var marker = new google.maps.Marker({\n" +
" position: position,\n" +
" map: map,\n" +
" title:'test point',\n " +
" });\n" +
" map.panTo(position);\n" +
"}");
答案 0 :(得分:1)
根据您发布的代码,executeJavaScriptAndReturnValue()
配置地图事件的侦听器。在这种情况下,您需要回调Java端并在收到地图事件后立即传递latLng
值。
以下是展示该方法的完整示例:
public class GoogleMapsSample {
public static void main(String[] args) {
final Browser browser = new Browser();
browser.addScriptContextListener(new ScriptContextAdapter() {
@Override
public void onScriptContextCreated(ScriptContextEvent event) {
browser.executeJavaScriptAndReturnValue("window").asObject().setProperty("java", new JavaCallback());
}
});
BrowserView view = new BrowserView(browser);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(view, BorderLayout.CENTER);
frame.setSize(900, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
// Provide the correct full path to the map.html file, e.g. D:\\map.html
Browser.invokeAndWaitFinishLoadingMainFrame(browser, new Callback<Browser>() {
@Override
public void invoke(Browser value) {
browser.loadURL("map.html");
}
});
browser.executeJavaScriptAndReturnValue(
"map.addListener('click', function(e) {\n" +
"placeMarker(e.latLng, map);\n" +
"});\n" +
"\n" +
"function placeMarker(position, map) {\n" +
" var marker = new google.maps.Marker({\n" +
" position: position,\n" +
" map: map,\n" +
" title:'test point',\n " +
" });\n" +
" map.panTo(position);\n" +
" window.java.onMarkerAdded(position);" +
"}");
}
public static class JavaCallback {
public void onMarkerAdded(JSObject position){
double lat = position.getProperty("lat").asFunction().invoke(position).asNumber().getDouble();
double lng = position.getProperty("lng").asFunction().invoke(position).asNumber().getDouble();
System.out.println("lat = " + lat + ", lng = " + lng);
}
}
}
可以与此示例一起使用的map.html
的HTML代码:
The code of our map.html file is the following:
HTML
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map-canvas { height: 100% }
</style>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=API_KEY&sensor=false"></script>
<script type="text/javascript">
var map;
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(48.209331, 16.381302),
zoom: 4
};
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>
要使用此文件,请将API_KEY替换为您的Google API密钥。有关如何获取API密钥的信息,请参阅instruction。
您可以在以下文章中找到从JavaScript回调到Java的一般说明: https://jxbrowser.support.teamdev.com/support/solutions/articles/9000013062-calling-java-from-javascript