0
我想在kml文件中創建一個彩色標記。Java創建帶有彩色標記的KML文件
並在谷歌地球上檢查它。
現在我的代碼是這樣的:
public class GenKMLPlaceMarker {
public int id;
public String name;
public String address;
public float lat;
public float lng;
public String type;
public static void main(String[] args) {
Statement stmt;
ResultSet rs;
GenKMLPlaceMarker KML = new GenKMLPlaceMarker();
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:디비폼";
Connection con = DriverManager.getConnection(url, "", "");
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer aTransformer = tranFactory.newTransformer();
Document doc = builder.newDocument();
Element root = doc.createElement("kml");
root.setAttribute("xmlns", "http://earth.google.com/kml/2.1");
doc.appendChild(root);
Element dnode = doc.createElement("Document");
root.appendChild(dnode);
//레스토랑 스타일 구성
Element rstyle = doc.createElement("Style");
rstyle.setAttribute("id", "restaurantStyle");
Element ristyle = doc.createElement("IconStyle");
ristyle.setAttribute("id", "restaurantIcon");
Element rcolor = doc.createElement("Color");// 아이콘에 색 부여하기????
Element ricon = doc.createElement("Icon");
Element riconhref = doc.createElement("href");
riconhref.appendChild(doc.createTextNode("http://maps.google.com/mapfiles/kml/pal2/icon63.png"));
rstyle.appendChild(ristyle);
ricon.appendChild(riconhref);
ristyle.appendChild(ricon);
dnode.appendChild(rstyle);
//바 스타일 구성
Element bstyle = doc.createElement("Style");
bstyle.setAttribute("id", "barStyle");
Element bistyle = doc.createElement("IconStyle");
bistyle.setAttribute("id", "barIcon");
Element bicon = doc.createElement("Icon");
Element biconhref = doc.createElement("href");
biconhref.appendChild(doc.createTextNode("http://maps.google.com/mapfiles/kml/pal2/icon27.png"));
bstyle.appendChild(bistyle);
bicon.appendChild(biconhref);
bistyle.appendChild(bicon);
dnode.appendChild(bstyle);
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * FROM markers");
while (rs.next()) {
KML.id = rs.getInt("id");
KML.name = rs.getString("name");
KML.address = rs.getString("address");
KML.lat = rs.getFloat("lat");
KML.lng = rs.getFloat("lng");
KML.type = rs.getString("type");
Element placemark = doc.createElement("Placemark");
dnode.appendChild(placemark);
Element name = doc.createElement("name");
name.appendChild(doc.createTextNode(KML.name));
placemark.appendChild(name);
Element descrip = doc.createElement("description");
descrip.appendChild(doc.createTextNode(KML.address));
placemark.appendChild(descrip);
Element styleUrl = doc.createElement("styleUrl");//스타일 설정하기 위해 선언하는 부분 아이콘으로 나타낸다
styleUrl.appendChild(doc.createTextNode("#" + KML.type+ "Style"));//bar restaurant 인지를 읽어와서 스타일 부여하는 부분
placemark.appendChild(styleUrl);
Element point = doc.createElement("Point");
Element coordinates = doc.createElement("coordinates");
coordinates.appendChild(doc.createTextNode(KML.lng + ","
+ KML.lat));
point.appendChild(coordinates);
placemark.appendChild(point);
}
Source src = new DOMSource(doc);
Result dest = new StreamResult(new File("c:/PlaceMarkers.kml"));
aTransformer.transform(src, dest);
System.out.println("Completed.....");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
建議你使用KML庫像JAK https://code.google.com/p/javaapiforkml/您的KML代而不是處理XML DOM API的。代碼將更小,更乾淨,運行速度更快。 – JasonM1