我想知道点击发生时如何从谷歌地图获取坐标到 JavaFX 应用程序?这是我尝试调用 Java 函数的代码示例:
来自 JavaScript 'click' 处理程序的 'showCoordinates':
public class JavaFXApplication extends Application {
public void showCoordinates(String coords)
{
System.out.println("Coordinates: " + coords);
}
@Override public void start(Stage stage)
{
final WebView webView = new WebView();
final WebEngine webEngine = webView.getEngine();
webEngine.load(getClass().getResource("googlemap.html").toString());
webEngine.getLoadWorker().stateProperty().addListener(
new ChangeListener<State>() {
@Override
public void changed(ObservableValue<? extends State> ov, State oldState, State newState) {
if (newState == State.SUCCEEDED) {
JSObject window = (JSObject) webEngine.executeScript("window");
window.setMember("java", new JavaFXApplication());
}
}
});
BorderPane root = new BorderPane();
root.setCenter(webView);
stage.setTitle("Google maps");
Scene scene = new Scene(root,1000,700, Color.web("#666970"));
stage.setScene(scene);
stage.show();
}
public static void main(String[] args){
Application.launch(args);
}
}
// googlemap.html file
<!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: 0px; padding: 0px }
#map_canvas { height: 100%; background-color: #666970; }
</style>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
function initialize() {
var latlng = new google.maps.LatLng(40.75089, -73.93804);
var myOptions = {
zoom: 10,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false,
panControl: true,
navigationControl: true,
streetViewControl: false,
backgroundColor: "#666970"
};
var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
document.map = map;
google.maps.event.addListener(map, 'click', function(event) {
//java.showCoordinates(event.latLng); ???
});
map.setCenter(location);
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="width:100%; height:100%"></div>
</body>
</html>