I drawed some lines and polygon on google map and I want to export this map as kml file , How can I do that ?
1 Answers
I put everything into a class while I'm parsing the kml coming in. I use the same class from my 1st app in this 2nd app. Just extended the original class. It's pretty cool to me to have 100% reuse of a class from another app and use the super opcode.
Anyways. Basically its a list of polyline Options and a list of marker options.
super.get_po which is the polylines option super.get_mo which is the marker options
Then I just serialize that class back out when I'm writing kml. My golf app depends on the order of markers and lines. For each hole It goes marker, line, marker and I use the same style as http://maps.google.com where each element has it's own style.
from my golf app which has a whopping earth shattering two downloads me and probably my wifes phone.
@Override
public void saveGolfMap() {
// TODO Auto-generated method stub
FileOutputStream fileos = null;
StringBuilder sb = new StringBuilder();
try {
fileos = new FileOutputStream(super.get_pathstring());
} catch (FileNotFoundException e) {
// Log.e("FileNotFoundException", e.toString());
e.printStackTrace();
}
// serialize the file();
try {
xmlSerializer = XmlPullParserFactory.newInstance().newSerializer();
} catch (XmlPullParserException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
xmlSerializer.setOutput(fileos, "UTF-8");
xmlSerializer.startDocument(null, null);
xmlSerializer.setFeature(
"http://xmlpull.org/v1/doc/features.html#indent-output",
true);
xmlSerializer.startTag(null, "kml");
xmlSerializer.attribute(null, "xmlns",
"http://earth.google.com/kml/2.2");
xmlSerializer.startTag(null, "Document");
xmlSerializer.startTag(null, "name");
xmlSerializer.text("todo put the name here");
xmlSerializer.endTag(null, "name");
xmlSerializer.startTag(null, "description");
xmlSerializer.cdsect("todo put the description here");
xmlSerializer.endTag(null, "description");
// spin through the marker options and create a style section for
// each two
// markers create three style sections
// <Style id="style1">
// <IconStyle>
// <Icon>
// <href>http://maps.gstatic.com/mapfiles/ms2/micons/golfer.png</href>
// </Icon>
// </IconStyle>
// </Style>
// <Style id="style2">
// <IconStyle>
// <Icon>
// <href>http://maps.gstatic.com/mapfiles/ms2/micons/flag.png</href>
// </Icon>
// </IconStyle>
// </Style>
// <Style id="style3">
// <LineStyle>
// <color>73FF0000</color>
// <width>3</width>
// </LineStyle>
// </Style>
setInt();
int y = 0;
for (int k = 0; k < mosize; k++) {
y += 1;
if (k % 2 == 0) {
// write out the tee marker style
xmlSerializer.startTag(null, "Style");
xmlSerializer.attribute(null, "id",
"style" + Integer.toString(y));
xmlSerializer.startTag(null, "IconStyle");
xmlSerializer.startTag(null, "Icon");
xmlSerializer.startTag(null, "href");
xmlSerializer
.text("http://maps.gstatic.com/mapfiles/ms2/micons/golfer.png");
xmlSerializer.endTag(null, "href");
xmlSerializer.endTag(null, "Icon");
xmlSerializer.endTag(null, "IconStyle");
xmlSerializer.endTag(null, "Style");
// write out the polyline style
y += 1;
xmlSerializer.startTag(null, "Style");
xmlSerializer.attribute(null, "id",
"style" + Integer.toString(y));
xmlSerializer.startTag(null, "LineStyle");
xmlSerializer.startTag(null, "color");
xmlSerializer.text("73FF0000");
xmlSerializer.endTag(null, "color");
xmlSerializer.startTag(null, "width");
xmlSerializer.text("10");
xmlSerializer.endTag(null, "width");
xmlSerializer.endTag(null, "LineStyle");
xmlSerializer.endTag(null, "Style");
} else {
// write out the cup marker style
xmlSerializer.startTag(null, "Style");
xmlSerializer.attribute(null, "id",
"style" + Integer.toString(y));
xmlSerializer.startTag(null, "IconStyle");
xmlSerializer.startTag(null, "Icon");
xmlSerializer.startTag(null, "href");
xmlSerializer
.text("http://maps.gstatic.com/mapfiles/ms2/micons/flag.png");
xmlSerializer.endTag(null, "href");
xmlSerializer.endTag(null, "Icon");
xmlSerializer.endTag(null, "IconStyle");
xmlSerializer.endTag(null, "Style");
}
}
// write placemark for tee
// write poly lines
// write placemark for cup
// <Placemark>
// <name>Tee Hole 1</name>
// <description><![CDATA[]]></description>
// <styleUrl>#style1</styleUrl>
// <Point>
// <coordinates>-xx.999999,xx.999999,0.000000</coordinates>
// </Point>
// </Placemark>
y = 0;
for (int k = 0; k < mosize; k++) {
y += 1;
// write out the tee or cup PlaceMark
mo = super.get_mo().get(k);
xmlSerializer.startTag(null, "Placemark");
xmlSerializer.startTag(null, "name");
xmlSerializer.text(mo.getTitle());
xmlSerializer.endTag(null, "name");
xmlSerializer.startTag(null, "description");
xmlSerializer.cdsect("");
xmlSerializer.endTag(null, "description");
xmlSerializer.startTag(null, "styleUrl");
xmlSerializer.text("#style" + Integer.toString(y));
xmlSerializer.endTag(null, "styleUrl");
xmlSerializer.startTag(null, "Point");
xmlSerializer.startTag(null, "coordinates");
ll = mo.getPosition();
xmlSerializer.text(Double.toString(ll.longitude) + ","
+ Double.toString(ll.latitude) + ",0.000000");
xmlSerializer.endTag(null, "coordinates");
xmlSerializer.endTag(null, "Point");
xmlSerializer.endTag(null, "Placemark");
// write out the polyline options
// <Placemark>
// <name>Hole One</name>
// <description><![CDATA[]]></description>
// <styleUrl>#style3</styleUrl>
// <LineString>
// <tessellate>1</tessellate>
// <coordinates>
// -xx.999999,xx.999999,0.000000
// -xx.999999,xx.999999,0.000000
// -xx.999999,xx.999999,0.000000
// -xx.9999999,xx.999999,0.000000
// -xx.9999999,xx.999999,0.000000
// </coordinates>
// </LineString>
// </Placemark>if (hole > 0){
if (k % 2 == 0) {
int z = k / 2;
if (z < posize) {
y += 1;
po = this.get_po().get(z);
xmlSerializer.startTag(null, "Placemark");
xmlSerializer.startTag(null, "name");
xmlSerializer.endTag(null, "name");
xmlSerializer.startTag(null, "description");
xmlSerializer.cdsect("");
xmlSerializer.endTag(null, "description");
xmlSerializer.startTag(null, "styleUrl");
xmlSerializer.text("#style" + Integer.toString(y));
xmlSerializer.endTag(null, "styleUrl");
xmlSerializer.startTag(null, "LineString");
xmlSerializer.startTag(null, "tessellate");
xmlSerializer.text("1");
xmlSerializer.endTag(null, "tessellate");
xmlSerializer.startTag(null, "coordinates");
sb.setLength(0);
for (LatLng ll3 : po.getPoints()) {
sb.append(Double.toString(ll3.longitude));
sb.append(",");
sb.append(Double.toString(ll3.latitude));
sb.append(",0.000000\n");
}
sb.setLength(sb.length() - 2);
String s = sb.toString();
xmlSerializer.text(s);
xmlSerializer.endTag(null, "coordinates");
xmlSerializer.endTag(null, "LineString");
xmlSerializer.endTag(null, "Placemark");
}
}
}
xmlSerializer.endTag(null, "Document");
xmlSerializer.endTag(null, "kml");
xmlSerializer.endDocument();
xmlSerializer.flush();
fileos.close();
} catch (Exception e) {
e.printStackTrace();
// Log.e("Exception", "Exception occured in wroting");
}
}
Good Luck

- 5,581
- 1
- 26
- 35
-
Just a note if you don't close a tag correctly the xmlserializer will complain with exception. – danny117 Aug 25 '13 at 21:00