我正在编写一个在JavaFX应用程序中显示Google Map的程序。以下是代码:
public class Window {
private static JFXPanel fxContainer;
MyBrowser myBrowser;
double lat;
double lon;
WindowGPSServer windowsgpsserver;
public static void main(String[] args) throws IOException {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Window().init();
}
});
}
public void init() {
fxContainer = new JFXPanel();
windowsgpsserver = new WindowGPSServer(fxContainer);
windowsgpsserver.setVisible(true);
// create JavaFX scene
Platform.runLater(new Runnable() {
@Override
public void run() {
//javaFX operations should go here
createScene();
}
});
}
private void createScene() {
myBrowser = new MyBrowser();
myBrowser.setCache(false);
Scene scene = new Scene(myBrowser);
fxContainer.setScene(scene);
this.windowsgpsserver.setMyBrowser(myBrowser);
}
}
和
public class MyBrowser extends Pane {
double lat;
double lon;
MyBrowser myBrowser;
WebView webView = new WebView();
WebEngine webEngine = webView.getEngine();
public MyBrowser() {
Toolkit tk = Toolkit.getDefaultToolkit();
int xSize = ((int) (tk.getScreenSize().getWidth()));
int ySize = ((int) tk.getScreenSize().getHeight());
xSize= new Double(xSize*0.75).intValue();
ySize= new Double(ySize*0.90).intValue();
webView.setPrefSize(xSize, ySize);
final URL urlGoogleMaps = getClass().getResource("demo.html");
webEngine.load(urlGoogleMaps.toExternalForm());
webEngine.setOnAlert(new EventHandler<WebEvent<String>>() {
@Override
public void handle(WebEvent<String> e) {
System.out.println(e.toString());
System.out.println(e.getData());
System.gc();
}
});
getChildren().add(webView);
}
public void ChangeLocation(String latit, String longi) {
lat = Double.parseDouble(latit);
lon = Double.parseDouble(longi);
webEngine.executeScript("" +
"window.lat = " + lat + ";" +
"window.lon = " + lon + ";" +
"document.goToLocation(window.lat, window.lon);"
);
}
}
和demo.html文件:
<body style="height: 100%;" onload="mapaStart()">
<script type="text/javascript">
var map;
function mapaStart() {
//document.map = new google.maps.Map(document.getElementById("mapcanvas"));
var latlng = new google.maps.LatLng(35.857908, 10.598997);
var Options = {
zoom: 15,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("mapcanvas"), Options);
//var carMarkerImage = new google.maps.MarkerImage('resources/images/car.png');
var marker = new google.maps.Marker({
position: new google.maps.LatLng(35.857908, 10.598997),
map: map,
draggable: false,
//icon: carMarkerImage,
title: "",
autoPan: true
});
var infobulle = new google.maps.InfoWindow({
content: "Aktualna pozycja"
});
google.maps.event.addListener(marker, 'mouseover', function() {
infobulle.open(map, marker);
});
document.goToLocation = function(x, y) {
alert("goToLocation, x: " + x +", y:" + y);
var latLng = new google.maps.LatLng(x, y);
marker.setPosition(latLng);
map.setCenter(latLng);
}
google.maps.event.addListener(map, 'zoom_changed', function() {
alert("ZOOM");
var MapOptions = {
scrollwheel: false
};
map.setOptions(MapOptions)
setTimeout(aaa, 1000);
});
}
function aaa() {
var MapOptions = {
scrollwheel: true
};
map.setOptions(MapOptions)
}
</script>
<div id="mapcanvas" style="width: 100%; height: 90%; border: 1px solid black; background: gray;">
</div>
</body>
</html>
fxContainer被添加到JPanel并显示在Swing应用程序中。 启动程序后一切正常。但是在放大和缩小几次后,程序变得没有响应(难以缩放,将地图移动到两侧)并显示以下错误:
“检测到未完成的资源锁定: D3D Vram Pool:268 433 488二手(100,0%),268 433 488管理(100,0%),268 435 456总计 共管理39项资源 平均资源年龄为0.1帧 最大支持年龄的0资源(0,000000) 35个资源标记为永久性(89,700000) 2个资源有不匹配的锁(5,100000) 锁定2个资源(5,100000) 35个资源包含有趣的数据(89,700000) 0资源消失(0,000000)“
有人可以帮我解决这个问题吗?
谢谢
马丁