我正在創建一個android應用程序,我需要創建具有位置標題,位置描述的KML文件&位置圖像。 截至目前我已經設法標題&說明添加到KML文件&它是工作完全正常,但我無法找到任何解決方案,我可以用JAVA添加圖片到KML文件。如何在KML/XML中使用CDATA標記添加圖像?
下面是創建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();
}
}
這裏是CDATA標籤內引用KML文件的片段具有圖像數據(託管在谷歌雲)所以,我認爲我們可以添加類似圖像從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數據庫&創建KML文件。
我也問過的問題爲KML/XML這是因爲,如果我們可以用XML創建那麼它有可能爲KML所以,對這個有什麼建議?
好吧,我已經通過https://developers.google.com/kml/documentation/kmzarchives?csw=1看起來它對我很有用。也許我需要單獨創建圖像文件夾,並將圖像的相對位置添加到kml文件。 –
您能否請建議使用java apis創建kmz文件示例? –
抱歉,我還沒有 – k3b