I'm creating an android application where I need to create KML file which have Location title , location description & Image of location . As of now I have managed to add Title & description to Kml file & it is working perfectly fine but I'm not able to find any solution where I can add Image into KML file using JAVA .
Here is code of creating KML file :
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();
}
}
And here is snippet of reference KML file which have image data (hosted on google cloud) within CDATA tag so , I thought we can add something like image data within CDATA tag from JAVA APIs:
<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>
All data i.e location name , location description , latitude , longitude & images are stored in SQLite db & creating KML file from these data.
I have asked question as KML / XML it is because if we can create in XML then it is possible for the KML so , any suggestions on this ?
If your question is "how to embed an image into kml"? I donot think that this is specified in google-s kml standard.
I have only seen kml-s that contain a link relative to the kml-file and kmz-file which is a zip file containing kml with relative links plus images-files.
Here is the spec how to create kmz files with relative links plus images-files
Maybe also interesting for you:
The open source android app FancyPlaces want to use zipped gpx files