简体   繁体   中英

How to make dynamic kml from java

I am new to KML I have to make a KML file that contains some information about a place and that data should be displayed in Google map. I have written a code in java which will generate a KML as an output but I have some problem, the KML is not generating. java.io.FileNotFoundException: c:\\PlaceMarkers.kml (Access is denied) this is my error.. This is what I have done so far..

    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());
    }
}

}

You can use Java API for KML
The objective of the Java API for KML is to provide Java interfaces for easy access to KML (Keyhole Markup Language) data.

The main goal of the Java API for KML (JAK) is to provide automatically generated full reference implementation of the KML object model defined by OGC's KML standard and Google's GX extensions. It is an object orientated API that enables the convenient and easy use of KML in existing Java environments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM