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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
/**
* Afficher une carte GoogleMap dans un JEditorPane
* @author fobec 2010
*/
public class JGoogleMapEditorPan extends JEditorPane {
private int zoomFactor = 7;
private String ApiKey = "";
private String roadmap = "roadmap";
public final String viewTerrain = "terrain";
public final String viewSatellite = "satellite";
public final String viewHybrid = "hybrid";
public final String viewRoadmap = "roadmap";
/**
* Constructeur: initialisation du EditorKit
*/
public JGoogleMapEditorPan() {
HTMLEditorKit kit = new HTMLEditorKit();
HTMLDocument htmlDoc = (HTMLDocument) kit.createDefaultDocument();
this.setEditable(false);
this.setContentType("text/html");
this.setEditorKit(kit);
this.setDocument(htmlDoc);
}
/**
* Fixer le zoom
* @param zoom valeur de 0 à 21
*/
public void setZoom(int zoom) {
this.zoomFactor = zoom;
}
/**
* Fixer la clé de developpeur
* @param key APIKey fourni par Google
*/
public void setApiKey(String key) {
this.ApiKey = key;
}
/**
* Fixer le type de vue
* @param roadMap
*/
public void setRoadmap(String roadMap) {
this.roadmap = roadMap;
}
/**
* Afficher la carte d'après des coordonnées GPS
* @param latitude
* @param longitude
* @param width
* @param height
* @throws Exception erreur si la APIKey est non renseignée
*/
public void showCoordinate(String latitude, String longitude, int width, int height) throws Exception {
this.setMap(latitude, longitude, width, height);
}
/**
* Afficher la carte d'après une ville et son pays
* @param city
* @param country
* @param width
* @param height
* @throws Exception erreur si la APIKey est non renseignée
*/
public void showLocation(String city, String country, int width, int height) throws Exception {
this.setMap(city, country, width, height);
}
/**
* Assembler l'url et Générer le code HTML
* @param x
* @param y
* @param width
* @param height
* @throws Exception
*/
private void setMap(String x, String y, Integer width, Integer height) throws Exception {
if (this.ApiKey.isEmpty()) {
throw new Exception("Developper API Key not set !!!!");
}
String url = "http://maps.google.com/maps/api/staticmap?";
url += "center=" + x + "," + y;
url += "&zoom=" + this.zoomFactor;
url += "&size=" + width.toString() + "x" + height.toString();
url += "&maptype=" + this.roadmap;
url += "&markers=color:blue" + x + "," + y;
url += "&sensor=false";
url += "&key=" + this.ApiKey;
String html = "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>";
html += "<html><head></head><body>";
html += "<img src='" + url + "'>";
html += "</body></html>";
this.setText(html);
}
} |
Partager