所以我试图在我用java编写的桌面应用程序中实现谷歌地图。事实证明,这比我想象的要困难得多,尤其是在我缺乏技能的情况下。我以前从未在这里提交过问题,但我会尝试按照我在研究时看到的方式来做。
这是我的java类:
package GUI;
import java.net.URL;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebEvent;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class JavaFXGoogleMaps extends Application {
private Scene scene;
MyBrowser myBrowser;
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("SHEEPTRACKER3000");
primaryStage.setWidth(400);
primaryStage.setHeight(300);
myBrowser = new MyBrowser();
scene = new Scene(myBrowser, 400, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
static class MyBrowser extends Region {
HBox toolbar;
static WebView webView = new WebView();
static WebEngine webEngine = webView.getEngine();
public MyBrowser() {
final URL urlGoogleMaps = getClass().getResource(
"GoogleMapsV3.html");
webEngine.load(urlGoogleMaps.toExternalForm());
webEngine.setJavaScriptEnabled(true);
getChildren().add(webView);
webEngine.setOnAlert(new EventHandler<WebEvent<String>>() {
@Override
public void handle(WebEvent<String> arg0) {
System.out.println("We are here");
webEngine
.executeScript("document.getElementById('test').innerHTML = 'the new text';");
webEngine.executeScript("document.setNewMarker()");
webEngine
.executeScript("document.setNewMarkerWithParameters(nAME-63.44-10.39)");
// webEngine.load(urlGoogleMaps.toExternalForm());
// getChildren().add(webView);
}
});
}
}
}
这是我的 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=AIzaSyAGiYaSPuB9Mx2t37gy9yhUR_QOUwdTWE0&sensor=false">
</script>
<script type="text/javascript">
function initialize() {
var myCenter = new google.maps.LatLng(63.43,10.39)
var mapOptions = {
center: myCenter,
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
var tilesloaded=true;
google.maps.event.addListenerOnce(map, 'tilesloaded', function(){
if(tilesloaded ==true){
window.alert();
tilesloaded=false;
}
});
}
document.setNewMarker = function setNewMarker(){
var marker=new google.maps.Marker({
position:new google.maps.LatLng(63.43,10.39),
icon:'sheep1.png',
map:map,
});
}
document.setNewMarkerWithParameters = function setNewMarkerWithParameters(info){
var array = info.split('-');
var a = array[0], b = array[1], c = array[2];
var lat = parseFloat(b);
var longi = parseFloat(c);
var name = parseString(a);
var marker=new google.maps.Marker({
position:new google.maps.LatLng(lat,longi),
icon:'sheep1.png',
map:map,
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<h1 id="test"> Does dis work?</h1>
<div id="map_canvas" style="width:400px; height:300px"></div>
</body>
</html>
我已经有一段时间了,学校的项目截止日期快到了。我收到错误消息“找不到变量'name'”,我不明白这是什么意思。为了把它放在上下文中,我试图在 java 中创建一个函数,我在 [name-lat-long] 表单上迭代一个数组并向地图添加标记。但是,当我收到错误消息时,我无法到达那里。尚未使用 name 变量,因为我正试图让这部分代码在我更进一步之前工作。但后来我希望能够单击标记并在该位置找到对象的名称。任何反馈都将不胜感激。