2

我是 KML 新手,我必须制作一个 KML 文件,其中包含有关某个地点的一些信息,并且该数据应显示在 Google 地图中。我在java中编写了一个代码,它将生成一个KML作为输出,但我有一些问题,KML没有生成。 java.io.FileNotFoundException: c:\PlaceMarkers.kml (Access is denied)这是我的错误..这是我到目前为止所做的..

    import java.io.*;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.transform.Result;
    import javax.xml.transform.Source;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;

    import org.w3c.dom.Document;
    import org.w3c.dom.Element;

   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("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/homeland";
        Connection con = DriverManager.getConnection(url, "root", "root");
        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 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"));
            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());
    }
}

}
4

1 回答 1

4

您可以将Java API 用于 KML 用于 KML
的 Java API 的目标是提供 Java 接口,以便轻松访问 KML(Keyhole 标记语言)数据。

Java API for KML (JAK) 的主要目标是提供由 OGC 的 KML 标准和 Google 的 GX 扩展定义的 KML 对象模型的自动生成的完整参考实现。它是一个面向对象的 API,可以在现有 Java 环境中方便、轻松地使用 KML。

于 2013-10-17T11:11:21.127 回答