【问题标题】:How to add Image using CDATA tag in KML / XML?如何在 KML/XML 中使用 CDATA 标签添加图像?
【发布时间】:2017-04-18 01:24:18
【问题描述】:

我正在创建一个 android 应用程序,我需要在其中创建具有位置标题、位置描述和位置图像的 KML 文件。 到目前为止,我已经设法将标题和描述添加到 Kml 文件中,并且运行良好,但我找不到任何可以使用 JAVA 将图像添加到 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 文件的 sn-p,它在 CDATA 标记中包含图像数据(托管在谷歌云上),所以我认为我们可以从 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 中,并根据这些数据创建 KML 文件。

我问过 KML / XML 的问题,这是因为如果我们可以在 XML 中创建,那么 KML 是可能的,对此有什么建议吗?

【问题讨论】:

    标签: java android xml kml


    【解决方案1】:

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

    我只见过包含相对于 kml 文件和 kmz 文件的链接的 kml-s,kmz 文件是一个 zip 文件,其中包含带有相对链接和图像文件的 kml。

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

    也许你也很感兴趣:

    开源android appFancyPlaces想用zipped gpx files

    【讨论】:

    【解决方案2】:

    尝试使用cDATA,也可以插入简单、字符串、url、html代码

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

    【讨论】:

    • 我已经更新了我的问题。图像存储在 android 设备上的 SQLite 数据库中。
    【解决方案3】:

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

    替换你的代码片段

    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

    【讨论】:

    • 感谢分享代码 sn-p 但我的图像存储在 android 设备上的数据库中,有什么方法可以在 CDATA 标记中添加图像?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-19
    • 1970-01-01
    • 1970-01-01
    • 2023-01-30
    • 1970-01-01
    相关资源
    最近更新 更多