Olá pessoal, bom dia
Estou com um problema, quero colocar um mapa (Google Maps), que irá montar a rota entre a empresa até o destino final, no caso meu cliente.
Bom, eu peguei a API do Google Maps, porém ela esta escrita em HTML e com funções em Javascript, então criei um JEditorPane e consegui colocar o código em HTML, mas o código em Javascript não rodou, até então eu não tinha o conhecimento que o Javascript não é suportado pelo JEditorPane, após este incoveniente eu fui atrás de algo que rodasse o Javascript dentro do Java, eu até achei o Rhino é uma API pelo que parece desenvolvida pelo Mozila, mas enfim.
Já tentei de tudo, não reparem na POG que fiz para gerar o HTML.
Será que uma boa alma a fim de me ajudar sabe como interagir o Javascript com o nosso querido Java?
segue o codigo do meu JEditorPane e o código da API Google Maps.
[size=16]JEditorPane[/size]
package Mapa;
import javax.swing.*;
import java.awt.*;
import javax.swing.text.*;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTML;
import java.io.IOException;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class MeuEditorPane extends JEditorPane {
HTML html;
HTMLDocument htmlDoc;
HTMLEditorKit htmlEdKit;
JFrame frame;
ScriptEngineManager factory = new ScriptEngineManager();
public MeuEditorPane() {
ScriptEngine engine = factory.getEngineByName("");
htmlEdKit = new HTMLEditorKit();
htmlDoc = (HTMLDocument) htmlEdKit.createDefaultDocument();
html = new HTML();
try {
htmlEdKit.insertHTML(htmlDoc, 0, montarString(), 0, 0, html.getTag("html"));
}
catch (IOException ioe) {
JOptionPane.showMessageDialog(null, "Exception: ioe");
System.out.print(montarString());
ioe.printStackTrace();
} catch (BadLocationException ble) {
JOptionPane.showMessageDialog(null, "Exception: ble");
};
setBorder(BorderFactory.createLineBorder(Color.yellow));
setContentType("text/plain");
setEditable(false);
setEditorKit(htmlEdKit);
setDocument(htmlDoc);
htmlEdKit.install(this);
}
private String montarString() {
String htmlCode = "<!DOCTYPE html>";
htmlCode = htmlCode + "<html>";
htmlCode = htmlCode + "<head>";
htmlCode = htmlCode + "<meta name=\"viewport\" content=\"initial-scale=1.0, user-scalable=no\"/>";
htmlCode = htmlCode + "<meta http-equiv=\"content-type\" content=\"text/html\"/>";
htmlCode = htmlCode + "<title>Google Maps JavaScript API v3 Example: Directions Simple</title>";
htmlCode = htmlCode + "<link href=\"http://code.google.com/apis/maps/documentation/javascript/examples/default.css\" rel=\"stylesheet\" type=\"text/css\" />";
htmlCode = htmlCode + "<script type=\"text/javascript\" src=\"http://maps.google.com/maps/api/js?sensor=false\"></script>";
htmlCode = htmlCode + "<script type=\"text/javascript\">";
htmlCode = htmlCode + "var directionDisplay;";
htmlCode = htmlCode + "var directionsService = new google.maps.DirectionsService();";
htmlCode = htmlCode + "var map;";
htmlCode = htmlCode + "function initialize() {";
htmlCode = htmlCode + " directionsDisplay = new google.maps.DirectionsRenderer();";
htmlCode = htmlCode + " var ipora = new google.maps.LatLng(-24.004562, -53.712995);";
htmlCode = htmlCode + " var myOptions = {";
htmlCode = htmlCode + " zoom:15,";
htmlCode = htmlCode + " mapTypeId: google.maps.MapTypeId.ROADMAP,";
htmlCode = htmlCode + " center: ipora";
htmlCode = htmlCode + " }";
htmlCode = htmlCode + " map = new google.maps.Map(document.getElementById(\"map_canvas\"), myOptions)";;
htmlCode = htmlCode + " directionsDisplay.setMap(map);";
htmlCode = htmlCode + "}";
htmlCode = htmlCode + "function initialize() {";
htmlCode = htmlCode + " directionsDisplay = new google.maps.DirectionsRenderer();";
htmlCode = htmlCode + " var ipora = new google.maps.LatLng(-24.004562, -53.712995);";
htmlCode = htmlCode + " var myOptions = {";
htmlCode = htmlCode + " zoom:15,";
htmlCode = htmlCode + " mapTypeId: google.maps.MapTypeId.ROADMAP,";
htmlCode = htmlCode + " center: ipora";
htmlCode = htmlCode + " }";
htmlCode = htmlCode + " map = new google.maps.Map(document.getElementById(\"map_canvas\"), myOptions);";
htmlCode = htmlCode + " directionsDisplay.setMap(map);";
htmlCode = htmlCode + "}";
htmlCode = htmlCode + "function calcRoute() {";
htmlCode = htmlCode + " var start = \"ipora,pr\";";
htmlCode = htmlCode + " var end = \"umuarama,pr\";";
htmlCode = htmlCode + " var request = {";
htmlCode = htmlCode + " origin:start,";
htmlCode = htmlCode + " destination:end,";
htmlCode = htmlCode + " travelMode: google.maps.DirectionsTravelMode.DRIVING";
htmlCode = htmlCode + " };";
htmlCode = htmlCode + " directionsService.route(request, function(response, status) {";
htmlCode = htmlCode + " if (status == google.maps.DirectionsStatus.OK) {";
htmlCode = htmlCode + " directionsDisplay.setDirections(response);";
htmlCode = htmlCode + " }";
htmlCode = htmlCode + " });";
htmlCode = htmlCode + "}";
htmlCode = htmlCode + "</script>";
htmlCode = htmlCode + "</head>";
htmlCode = htmlCode + "<body onload=\"initialize()\">";
htmlCode = htmlCode + "<div>";
htmlCode = htmlCode + "<b>Start: </b>";
htmlCode = htmlCode + "<select id=\"start\" onchange=\"calcRoute();\">";
htmlCode = htmlCode + "<option value=\"ipora, pr\">Iporã, PR</option>";
htmlCode = htmlCode + "<option value=\"francisco alves, pr\">Fco Alves, PR</option>";
htmlCode = htmlCode + "<option value=\"umuarama, pr\">Umuarama, PR</option>";
htmlCode = htmlCode + "<option value=\"londrina, pr\">Londrina, PR</option>";
htmlCode = htmlCode + "<option value=\"curitiba, pr\">Curitiba, PR</option>";
htmlCode = htmlCode + "<option value=\"avenida rio de janeiro, umuarama, pr\">Av. Rio de Janeiro - Umuarama, PR</option>";
htmlCode = htmlCode + "</select>";
htmlCode = htmlCode + "<b>End: </b>";
htmlCode = htmlCode + "<select id=\"end\" onchange=\"calcRoute();\">";
htmlCode = htmlCode + "<option value=\"ipora, pr\">Iporã, PR</option>";
htmlCode = htmlCode + "<option value=\"francisco alves, pr\">Fco Alves, PR</option>";
htmlCode = htmlCode + "<option value=\"umuarama, pr\">Umuarama, PR</option>";
htmlCode = htmlCode + "<option value=\"londrina, pr\">Londrina, PR</option>";
htmlCode = htmlCode + "<option value=\"curitiba, pr\">Curitiba, PR</option>";
htmlCode = htmlCode + "<option value=\"avenida rio de janeiro, umuarama, pr\">Av. Rio de Janeiro - Umuarama, PR</option>";
htmlCode = htmlCode + "</select>";
htmlCode = htmlCode + "</div>";
htmlCode = htmlCode + "<div id=\"map_canvas\"></div>";
htmlCode = htmlCode + "</body>";
htmlCode = htmlCode + "</html>";
return htmlCode;
}
}
Obrigado pessoal.