如何在 KML / XML 中使用 CDATA 标签添加图像?
How to add Image using CDATA tag in KML / XML?
我正在创建一个 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 文件的片段,它在 CDATA 标签内有图像数据(托管在 google 云上)所以,我认为我们可以在来自 JAVA 的 CDATA 标签内添加类似图像数据的内容API:
<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 数据库中,并根据这些数据创建 KML 文件。
我问的问题是 KML / XML 是因为如果我们可以在 XML 中创建那么 KML 是可能的,那么对此有什么建议吗?
尝试使用 cDATA,您可以插入简单的字符串、url 和 html 代码
<description>
<![CDATA[
<img src=image-url />
<href>image-url</href>
]]>
</description>
如果你的问题是"how to embed an image into kml"?我不认为这是在 google-s kml 标准中指定的。
我只看到 kml-s 包含一个 link 相对于 kml 文件和 kmz 文件,这是一个包含 kml 和相对 links 加上图像文件的 zip 文件。
这是规范 how to create kmz files with relative links plus images-files
也许你也感兴趣:
开源 android 应用 FancyPlaces want to use zipped gpx files
KML 完全不同。如果你想添加 CDDATA 那么你可以使用下面的代码片段。
替换你的代码片段
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);
您也可以参考以下链接
- java adding cdata to xml string
- Creation of CDATA section is confusing
我正在创建一个 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 文件的片段,它在 CDATA 标签内有图像数据(托管在 google 云上)所以,我认为我们可以在来自 JAVA 的 CDATA 标签内添加类似图像数据的内容API:
<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 数据库中,并根据这些数据创建 KML 文件。
我问的问题是 KML / XML 是因为如果我们可以在 XML 中创建那么 KML 是可能的,那么对此有什么建议吗?
尝试使用 cDATA,您可以插入简单的字符串、url 和 html 代码
<description>
<![CDATA[
<img src=image-url />
<href>image-url</href>
]]>
</description>
如果你的问题是"how to embed an image into kml"?我不认为这是在 google-s kml 标准中指定的。
我只看到 kml-s 包含一个 link 相对于 kml 文件和 kmz 文件,这是一个包含 kml 和相对 links 加上图像文件的 zip 文件。
这是规范 how to create kmz files with relative links plus images-files
也许你也感兴趣:
开源 android 应用 FancyPlaces want to use zipped gpx files
KML 完全不同。如果你想添加 CDDATA 那么你可以使用下面的代码片段。
替换你的代码片段
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);
您也可以参考以下链接
- java adding cdata to xml string
- Creation of CDATA section is confusing