如何在KML / XML中使用CDATA标签添加图像?

时间:2017-04-18 01:24:19

标签: java android xml kml

我正在创建一个Android应用程序,我需要创建具有位置标题,位置描述和放大器的KML文件。位置的图像。 截至目前,我已设法添加标题& Kml文件的描述&它工作得很好,但是我无法找到任何可以使用JAVA将Image添加到KML文件中的解决方案。

以下是创建KML文件的代码:

public void generateKMLFile()
{
    try {
    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);

    Cursor c = mDatabaseManager.getAllLocations();
    while (c.moveToNext())
    {
        LocationBean lb = new LocationBean();
        lb.locationTitle = c.getString(c.getColumnIndex(DatabaseManager.LOCATION_TITLE));
        lb.lat = c.getDouble(c.getColumnIndex(DatabaseManager.LOCATION_LATITUDE));
        lb.log = c.getDouble(c.getColumnIndex(DatabaseManager.LOCATION_LONGITUDE));
        lb.remark = c.getString(c.getColumnIndex(DatabaseManager.LOCATION_REMARK));


        Element placemark = doc.createElement("Placemark");
        dnode.appendChild(placemark);
        Element name = doc.createElement("name");
        name.appendChild(doc.createTextNode(lb.locationTitle));
        placemark.appendChild(name);
        Element descrip = doc.createElement("description");
        descrip.appendChild(doc.createTextNode(lb.remark));
        placemark.appendChild(descrip);
        Element styleUrl = doc.createElement("styleUrl");
        styleUrl.appendChild(doc.createTextNode( "#" +lb.locationTitle+ "Style"));
        placemark.appendChild(styleUrl);
        Element point = doc.createElement("Point");
        Element coordinates = doc.createElement("coordinates");
        coordinates.appendChild(doc.createTextNode(lb.log+ "," + lb.lat));
        point.appendChild(coordinates);
        placemark.appendChild(point);

    }

    Source src = new DOMSource(doc);
    Result dest = new StreamResult(new File("/sdcard/PlaceMarkers.kml")); // temporarily directly saved to sdcard
    aTransformer.transform(src, dest);
    System.out.println("Completed.....");


}
catch (ParserConfigurationException e) {
    e.printStackTrace();
} catch (TransformerConfigurationException e) {
    e.printStackTrace();
} catch (TransformerException e) {
    e.printStackTrace();
}


}

这里是参考KML文件的片段,其中包含CDATA标签内的图像数据(托管在Google云端),因此,我认为我们可以在JAVA API的CDATA标签中添加类似图像数据的内容:

<Placemark>
    <name>Home</name>
    <description><![CDATA[<img src="https://lh4.googleusercontent.com/6-eAD3dYM1l4nBR5lQOZ3XLWmPuZ0juf4kgPqQykrJw0rTIo17TZQIc_1G1Jg4AuAV2M5xB4HBHB6x4WaHG4H4zQXCqeKonhmHm05AqLZsbhlIAmQyzFfzfpTaDmoVzoLQ" height="200" width="auto" /><br><br>My HOme]]></description>
    <styleUrl>#icon-1899-0288D1</styleUrl>
    <ExtendedData>
      <Data name="gx_media_links">
        <value>https://lh4.googleusercontent.com/6-eAD3dYM1l4nBR5lQOZ3XLWmPuZ0juf4kgPqQykrJw0rTIo17TZQIc_1G1Jg4AuAV2M5xB4HBHB6x4WaHG4H4zQXCqeKonhmHm05AqLZsbhlIAmQyzFfzfpTaDmoVzoLQ</value>
      </Data>
    </ExtendedData>
    <Point>
      <coordinates>
        73.1988519,22.2953976,0
      </coordinates>
    </Point>
  </Placemark>

所有数据,即位置名称,位置描述,纬度,经度和图像存储在SQLite db&amp;从这些数据创建KML文件。

我问过KML / XML问题是因为如果我们可以用XML创建那么KML就可以了,对此有什么建议吗?

3 个答案:

答案 0 :(得分:1)

如果您的问题是&#34;如何将图像嵌入kml&#34;?我不认为这是在google-s kml标准中指定的。

我只看到kml-s包含相对于kml文件和kmz文件的链接,这是一个包含kml及相对链接和图像文件的zip文件。

以下是规范how to create kmz files with relative links plus images-files

也许对你有意思:

开源Android应用FancyPlaces想要使用zipped gpx files

答案 1 :(得分:0)

尝试使用cDATA,您也可以插入简单,字符串,网址和HTML代码

<description>
    <![CDATA[
      <img src=image-url />
      <href>image-url</href>
    ]]>
</description>

答案 2 :(得分:0)

KML有一个不同的标准。如果你想添加CDDATA,那么你可以使用下面的代码片段。

替换你的代码Snippit

Element descrip = doc.createElement("description");
descrip.appendChild(doc.createTextNode(lb.remark));
placemark.appendChild(descrip); 

以下代码。

Node de = doc.createElement("description");
de.appendChild(doc.createCDATASection("<img src='https://lh4.googleusercontent.com/6-eAD3dYM1l4nBR5lQOZ3XLWmPuZ0juf4kgPqQykrJw0rTIo17TZQIc_1G1Jg4AuAV2M5xB4HBHB6x4WaHG4H4zQXCqeKonhmHm05AqLZsbhlIAmQyzFfzfpTaDmoVzoLQ' height='200' width='auto' /><br><br>My HOme"));
placemark.appendChild(de);

您还可以参考以下链接

  1. java adding cdata to xml string
  2. Creation of CDATA section is confusing
相关问题