我正嘗試使用Google Maps Api + JavaFx創建地圖。在主類中,我讀取JSON文件,然後創建聚類,程序的要點是將這些聚類顯示爲具有多個位置的標記。 因此,在主類中,我讀取JSON文件,然後創建DBSCANClusterer clusterer = new DBSCANClusterer(coordinates, 2, 2);
,我必須將其推送到類GoogleMap
,其中JavaFX創建webView
等等。此外,我用這個在我的HTML文件中使用Java代碼的腳本:如何創建JavaFx應用程序的實例並推送自己的參數?
JSObject jsobj = (JSObject) webView.getEngine()
.executeScript("window");
jsobj.setMember("BrowserJavaObject", new BrowserJavaObject(clusterer));
然後同樣的事情必須在BrowserJavaObject
類,所有的集羣計算是進一步推高。 我試圖創建GoogleMap
類的對象,但這不起作用。 那麼如何讓它工作?或者甚至可能嗎?謝謝你的建議。
JSONMain類:
public class JsonMain {
static List<Coordinate> coordinates = new ArrayList<>();
private static final String ITEMS_NAME = "items";
private static final String LATITUDE_PROPERTY = "latitude";
private static final String LONGITUDE_PROPERTY = "longitude";
private static final String CRASH_NAME = "em_type_name";
static void parseCrashCoordinates(final JsonReader jsonReader, final ICoordinatesListener listener)
throws IOException {
// Reading JSON file
}
// Collecting all coordinates in ArrayList.
private static void testCollecting()
throws IOException {
// List<Coordinate> coordinates = new ArrayList<>();
readAndParse((lat, lng) -> coordinates.add(new Coordinate(lat, lng)));
System.out.println(coordinates.size());
}
public static void main(String[] args) throws IOException, URISyntaxException {
testCollecting();
// Initialize our clustering class with locations, minimum points in cluster and max Distance
DBSCANClusterer clusterer = new DBSCANClusterer(coordinates, 2, 2);
GoogleMap gm = new GoogleMap(clusterer);
gm.launch(GoogleMap.class);
}
GoogleMap的類:
public class GoogleMap extends Application {
private DBSCANClusterer clusterer ;
public GoogleMap(DBSCANClusterer c) {
this.clusterer = c;
}
@Override
public void start(Stage stage) throws MalformedURLException {
File file = new File("C:/Users/Evgeny/git/Diploma_MSU/diploma/html/map.html");
URL url222 = file.toURI().toURL();
WebView webView = new WebView();
final WebEngine webEngine = webView.getEngine();
JSObject jsobj = (JSObject) webView.getEngine()
.executeScript("window");
jsobj.setMember("BrowserJavaObject", new BrowserJavaObject(clusterer));
webEngine.load(url222.toString());
// create scene
stage.setTitle("Web Map");
Scene scene = new Scene(webView,1000,700, Color.web("#666970"));
stage.setScene(scene);
// show stage
stage.show();
}
}
BrowserJavaObject類:
public class BrowserJavaObject {
private DBSCANClusterer clusterer ;
public BrowserJavaObject(DBSCANClusterer c) {
this.clusterer = c;
}
public String showMarkers() {
ArrayList<ArrayList<Coordinate>> clusters_raw= this.clusterer.performClustering();
ArrayList<Cluster> clusters = new ArrayList<>();
String pointsArray = " [ ";
for(int i=0; i<clusters_raw.size(); i++) {
Cluster c = new Cluster(clusters_raw.get(i));
clusters.add(c);
pointsArray += c.getLocationAsArray() + " , ";
}
pointsArray += "]";
System.out.println("Number Of Clusters Created: "+clusters.size());
return pointsArray;
}
}
個map.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Map</title>
<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 async defer type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?callback=loadmap">
</script>
</head>
<body onload="loadmap()">
<div id="map_canvas" style="width:100%; height:100%"></div>
<script>
var map;
function loadmap(){
var options = {
zoom: 16,
center: new google.maps.LatLng(55.7558, 37.6173),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), options);
var markers = [];
// Call the Java code to calculate clusters data, and save the returned clusters into a variable
var markers_data = BrowserJavaObject.showMarkers();
for(var i=0; i<markers_data.length; i++){
var position = markers_data[i];
var icon = "data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%2238%22%20height%3D%2238%22%20viewBox%3D%220%200%2038%2038%22%3E%3Cpath%20fill%3D%22%23a22%22%20stroke%3D%22%23ccc%22%20stroke-width%3D%22.5%22%20d%3D%22M34.305%2016.234c0%208.83-15.148%2019.158-15.148%2019.158S3.507%2025.065%203.507%2016.1c0-8.505%206.894-14.304%2015.4-14.304%208.504%200%2015.398%205.933%2015.398%2014.438z%22%2F%3E%3Ctext%20transform%3D%22translate%2819%2018.5%29%22%20fill%3D%22%23fff%22%20style%3D%22font-family%3A%20Arial%2C%20sans-serif%3Bfont-weight%3Abold%3Btext-align%3Acenter%3B%22%20font-size%3D%2212%22%20text-anchor%3D%22middle%22%3E"
+ position[2]
+ "%3C%2Ftext%3E%3C%2Fsvg%3E";
// if(zoom > 11) icon = null; //Default to marker with no number if at city zom level
markers.push(new google.maps.Marker({
position: new google.maps.LatLng(position[0], position[1]),
map: map,
title: position[2],
text: position[2],
icon: icon
})
);
}
}
</script>
</body>
</html>
所以地圖加載,但沒有集羣存在。最終所有這些聚類方法都不會被調用,因爲我的clusterer
對象沒有在其他類中進一步傳遞。
非常感謝!
這太長了。考慮發佈[mcve] – c0der