我想知道在点击时如何从Google Map到JavaFX应用程序获取坐标 发生了?以下是我尝试调用Java函数的代码示例:
' showCoordinates'来自JavaScript'点击'处理程序:
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>
答案 0 :(得分:1)
经过一些实验后,我找到了一个基于计时器的简单解决方案。这个想法是定时器函数定期轮询javascript文档并解析结果。
var latlng = new google.maps.LatLng(52.3, 4.59 );
var myOptions = {
zoom: 9,
center: latlng,
mapTypeId: google.maps.MapTypeId.TERRAIN,
mapTypeControl: true,
navigationControl: true,
streetViewControl: true,
backgroundColor: "#FFFFFF"
};
document.geocoder = new google.maps.Geocoder();
document.map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
document.updateJavaFX = function updateJavaFX() {
document.currentCenter = document.map.getCenter();
document.currentBounds = document.map.getBounds();
document.currentHeading = document.map.getHeading();
document.currentZoom = document.map.getZoom();
}
当调用updateJavaFX()函数时,文档变量(位置的currentCenter)被填充。
在JavaFX代码中,以下部分(此部分在AnimationTimer中调用)用于检索地图的当前纬度位置。
if ((mMapAutoUpdate.get()) && (mLastNow < pNow - 10000000)) {
mLastNow = pNow;
mMapView.getEngine().executeScript("document.updateJavaFX()");
JSObject jdoc = (JSObject) mMapView.getEngine().getDocument();
if (jdoc != null) {
try {
JSObject ds = (JSObject) jdoc.getMember("currentCenter");
String cc = ds.toString().trim();
cc = cc.substring(1, cc.length() - 1);
String[] ccs = cc.split(",");
double newlat = Double.parseDouble(ccs[0].trim());
double newlon = Double.parseDouble(ccs[1].trim());
mCenterLat.set(newlat);
mCenterLon.set(newlon);
int zoom = (Integer) jdoc.getMember("currentZoom");
mZoom.set(zoom);
} catch (ClassCastException cce) {
String ds = (String) jdoc.getMember("currentCenter");
String[] ccs = ds.trim().split(",");
double newlon = Double.parseDouble(ccs[0].substring(4).trim());
double newlat = Double.parseDouble(ccs[1].substring(4).trim());
mCenterLat.set(newlat);
mCenterLon.set(newlon);
int zoom = (Integer) jdoc.getMember("currentZoom");
mZoom.set(zoom);
}
}
}
我为可能的解决方案制作了博客条目;
http://oldguardprogrammer.blogspot.nl/2013/01/javafx-and-google-maps.html