webEngine executescript et tableau à envoyer
Bonjour,
j'essaye en vain d'envoyer un tableau initialement en Double[][] via webEngine.executeScript (" nomdemafonctionjvscript (' + montableau +')") malheureusement ça ne marche pas . Pourtant mon code javascript de la page HTML est correct.
Ma question est donc : Peut on envoyer un tableau de double ou de String ,si cela pose problème, via webEngine et executeScript à une fonction javascript se trouvant dans une page HTML ? Avez vous un petit exemple afin que je puisse calquer mon code dessus ?
En vous remerciant d'avance
Voici une partie du code java:
Code:
1 2 3 4 5 6 7
| final URL urlHello = getClass().getResource("leaflet.html");
webEngine.load(urlHello.toExternalForm());
double[][] untrace = {{49.84653688680709,2.263697236776352},{49.84661125902917,2.2638890147209167},{49.84654034598273, 2.264016419649124},{49.84607767904088,2.2648251056671143}
,{49.845558794959594,2.264261841773987},{49.845354699028064, 2.263827323913574},{49.84592893284039,2.2626739740371704},{49.84653688680709,2.263697236776352}};
webEngine.executeScript( " montrace(' " + untrace + " ') " ); // envoie via webengine |
Voici mon code html (programme leaflet issu de la bibliotheque du même nom (D'ailleurs un petit tuto dessus pour ceux qui ne connaissent pas l anglais ça serait super...):
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| <!DOCTYPE html>
<html>
<head>
<title>Leaflet Web Map</title>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
<style>
#map {
width: 960px;
height:500px;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
function montrace (toUnion){
var map = L.map('map',{
center: [49.846535, 2.26388],
zoom: 15
});
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
var marker = L.marker([49.846535,2.26388]);
L.polyline(toUnion,{color:'red',opacity:1}).addTo(map);
}
</script>
</body>
</html> |
Merci pour vos remarques voici la solution
J'ai donc traduit mon tableau java en String en séparant latitude et longitude par ";" et les coordonnées entre elles par "!" puis envoie à la fonction javascript avec recréation d'un tableau en prenant bien soins de "parser" dans la création du tableau sinon les valeurs sont prises pour du texte!!! (j ai passé une soirée à comprendre le problème!)
Voici le code complet javaScript:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| <!DOCTYPE html>
<html>
<head>
<title>Leaflet Web Map</title>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
<style>
#map {
width: 960px;
height:500px;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
function montrace (param1){
var map = L.map('map',{
center: [49.846535, 2.26388],
zoom: 15
});
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
var marker = L.marker([49.846535,2.26388]);
L.polyline(param1,{color:'red',opacity:1}).addTo(map);
}
function montranslator(user){
var compteur=0;
// pour dimensionner le futur tableau
for (i=0;i<user.length;i++){
if (user.charAt(i)==";") {compteur++;}
}
var traduit = new Array ();
var coordo = new Array();
coordo[0]="";coordo[1]="";
for (i=0;i<compteur;i++){
traduit[i] = new Array();
}
var a=0; var b=0;
for (i=0;i<user.length;i++){
if ((user.charAt(i)!=";")&&(user.charAt(i)!="!")) {coordo[b]=coordo[b]+ user.charAt(i);}
if (user.charAt(i)==";") {b++;}
if (user.charAt(i) =="!") {b=0;traduit[a][0]=parseFloat(coordo[0]);traduit[a][1]=parseFloat(coordo[1]);a++;coordo[0]="";coordo[1]="";}
}
montrace(traduit);
}
</script>
</body>
</html> |
et le code java un peu remanié ...
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
import java.io.File;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class main extends Application {
double[][] coordodouble = {{49.84653688680709,2.263697236776352},{49.84661125902917,2.2638890147209167},{49.84654034598273, 2.264016419649124},{49.84607767904088,2.2648251056671143}
,{49.845558794959594,2.264261841773987},{49.845354699028064, 2.263827323913574},{49.84592893284039,2.2626739740371704},{49.84653688680709,2.263697236776352}};
String coordoString = "";
@Override
public void start(Stage primaryStage) {
String envoi = "[49.846535, 2.26388]";
Button btn = new Button();
btn.setText("tracer");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
for (int i=0;i<coordodouble.length;i++){//modifier la longueur de la chaine
for (int j=0;j<2;j++){
coordoString=coordoString+String.valueOf(coordodouble[i][j]);
if (j<1){coordoString=coordoString+";";}
}
coordoString=coordoString+"!";
}
System.out.println(coordoString);
if (webengine != null)
{
webengine.executeScript("montranslator(' " + coordoString + " ')");
}
}
});
publishServices();
StackPane root = new StackPane();
HBox hh = new HBox();
hh.getChildren().add(btn);
hh.getChildren().add(webview);
root.getChildren().add(hh);
Scene scene = new Scene(root, 700, 450);
primaryStage.setTitle("essai Leaflet");
primaryStage.setScene(scene);
primaryStage.show();
}
private WebEngine webengine;
private static WebView webview;
private void publishServices() {
try {
webview = new WebView();
webview.setVisible(true);
webengine = webview.getEngine();
webengine.setJavaScriptEnabled(true);
File file = new File("C:\\Users\\....le trajet du fichier html...\\leaflet.html");
System.out.println(file.exists() + " file exitence");
webengine.load(file.toURI().toURL().toString());
} catch (Exception ex) {
System.err.print("error " + ex.getMessage());
ex.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
} |
Merci encore Monsieur BOUYE::):):)