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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
package main;
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 static final long serialVersionUID = 1L;
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);
}
/**
* Exemple : JGoogleMapEditorPan dans un JFrame
*/
public static void main(String[] args) {
JGoogleMapEditorPan googleMap = new JGoogleMapEditorPan();
try {
googleMap.setApiKey("Key google api");
// googleMap.setRoadmap(googleMap.viewHybrid);
/**
Afficher la ville de Strabourg
*/
googleMap.showLocation("strasbourg", "france", 390, 400);
/**
* Afficher Paris en fonction ses coordonnées GPS
*/
// googleMap.showCoordinate("48.8667", "2.3333",390, 400);
} catch (Exception ex) {
Logger.getLogger(JGoogleMapEditorPan.class.getName()).log(Level.SEVERE, null, ex);
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(googleMap);
frame.setSize(400, 420);
frame.setLocation(200, 200);
frame.setVisible(true);
}
} |
Partager