0

我想在 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());
    }
}
 }
4

1 回答 1

0

您必须像下面的示例(红色标记)一样将颜色元素添加到您的 IconStyle 中。至少这是当您更改图标颜色并保存为 kml 时谷歌地图生成的内容,而我在您的代码中没有看到类似的内容。

<Style id="stackOverflowExample_dining">
    <IconStyle>
        <color>ff0000ff</color>
        <scale>1.4</scale>
        <Icon>
            <href>http://maps.google.com/mapfiles/kml/shapes/dining.png</href>
        </Icon>
        <hotSpot x="0.5" y="0" xunits="fraction" yunits="fraction"/>
    </IconStyle>
    <ListStyle>
    </ListStyle>
</Style>
于 2013-08-01T11:34:54.137 回答