2017-10-08 8 views
0

J'essaie de créer une carte en utilisant Google Maps Api + JavaFx. En classe principale, je lis le fichier JSON puis crée des clusters et le point de programme est de montrer ces clusters comme des marqueurs avec le nombre d'emplacements dans celui-ci. Donc dans la classe principale, j'ai lu le fichier JSON, puis créer DBSCANClusterer clusterer = new DBSCANClusterer(coordinates, 2, 2); et je dois pousser dans la classe GoogleMap, où le JavaFX crée webView et ainsi de suite. Je également utiliser cette option pour utiliser le code java dans le script dans mon fichier html:Comment créer une instance d'application JavaFx et y ajouter ses propres paramètres?

JSObject jsobj = (JSObject) webView.getEngine() 
      .executeScript("window"); 
    jsobj.setMember("BrowserJavaObject", new BrowserJavaObject(clusterer)); 

Puis la même chose doivent être poussé plus loin dans BrowserJavaObject classe où tous les calculs de regroupement sont. J'ai essayé de créer l'objet de la classe GoogleMap, mais cela ne fonctionne pas. Alors, comment le faire fonctionner? Ou est-ce même possible? Merci pour le conseil.

classe 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); 
    } 

classe 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(); 

} 
} 

classe 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> 

Donc, la carte est chargée, mais pas de groupes là-bas. Et finalement que toutes ces méthodes de clustering ne sont pas invoquées, parce que mon objet clusterer n'est pas passé plus loin dans les autres classes.

Merci beaucoup!

+0

C'est beaucoup trop long. Envisager de poster [mcve] – c0der

Répondre

1

Vous utilisez la classe Application dans le mauvais sens. La classe Application représente l'ensemble de votre application et est responsable de son cycle de vie; en particulier, il a une méthode start() qui est appelée pour vous lorsque le toolkit JavaFX est démarré par un appel à launch(). La classe Application ne représente pas une partie particulière de votre interface utilisateur (ce à quoi correspond votre classe GoogleMap).

Vous devriez faire JsonMain une sous-classe de Application, non GoogleMap, et vous devez déplacer le code de démarrage à la méthode start() (bien nommé):

public class GoogleMap { 

    private DBSCANClusterer clusterer ; 

    private final WebView view ; 

    public GoogleMap(DBSCANClusterer c) throws MalformedURLException { 
     this.clusterer = c; 

     File file = new File("C:/Users/Evgeny/git/Diploma_MSU/diploma/html/map.html"); 
     URL url222 = file.toURI().toURL(); 

     view = new WebView(); 
     final WebEngine webEngine = webView.getEngine(); 

     JSObject jsobj = (JSObject) webView.getEngine() 
       .executeScript("window"); 
     jsobj.setMember("BrowserJavaObject", new BrowserJavaObject(clusterer)); 

     webEngine.load(url222.toString()); 

    } 

    public Node getView() { 
     return view ; 
    } 

} 

et

public class JsonMain extends Application { 

    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) { 
     launch(args); 
    } 

    @Override 
    public void start(Stage stage) 
     throws IOException, URISyntaxException, MalformedURLException { 

     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); 

     // create scene 
     stage.setTitle("Web Map"); 
     Scene scene = new Scene(gm.getView(), 1000, 700, Color.web("#666970")); 
     stage.setScene(scene); 
     // show stage 
     stage.show(); 

    } 

} 

Vous devrait probablement refactoriser davantage afin de bien séparer vos préoccupations; par exemple. Ce n'est probablement pas le travail de votre classe Application à analyser, mais cela vous permettra au moins de transmettre des paramètres à votre classe GoogleMap et de démarrer l'application de la manière prévue.